Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested struct as members in a class

I am writing a class which holds 90 integer variables. There are basically 10 but each has 3x3 attributes, making them 90. I'll explain it further.

So the 10 variables I have are: v0, v1, v2, ..., v9. Each of these has 3 different values for city, county and state. Also, each of these has 3 different values for past, present and future.

So, a typical value would be: v3.City.Future or v7.State.Past or v8.County.Present.

I was thinking to implement a class, with 10 structs for each variable, then each of those struct has a 3 nested structs (for city, county and state) and each of those structs has 3 integer variables for past, present and future.

This is still in conceptual phase. I tried to go with the above mentioned approach but it is not working. Please feel free to give any suggestions.

Thanks.

UPDATE:

Here is the code snippet:

public class MyClass
{
    public MyVariable myvariable;
}

public struct MyVariable
{
    public struct National
    {
        public int Past { get; set; }
        public int Present { get; set; }
        public int Future { get; set; }
    }
    ...
}

Now when I instantiate the class and access the member (myclass.myvariable.National.Past), it does not get the member.

like image 521
Farhan Avatar asked Nov 29 '22 02:11

Farhan


1 Answers

Here is the answer to this:

public struct PeriodVars
{
    public int Past { get; set; }
    public int Present { get; set; }
    public int Future { get; set; }
}

public struct VarStruct
{
    public PeriodVars City;
    public PeriodVars County;
    public PeriodVars State;
}

Now the class will be:

public class MyClass
{
    public VarStruct v0;
    public VarStruct v1;
    ...
}

Once I instantiate an object of the class, I can refer it to like this:

objMyClass.v0.City.Past = 111;

There wasn't much help I got by posting it here besides criticism.

Often, one person's inability to understand is seen as another person's inability to explain. I spent 15-20 minutes writing the question and people started down-voting it in less than 30 seconds.

like image 177
Farhan Avatar answered Dec 05 '22 03:12

Farhan