nested struct as members in a class

17,320

Solution 1

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.

Solution 2

Looks like you are mixing types and instances based on your comment:

Assuming your MyVariable looks following:

public struct MyVariable
{
    public struct National
    {
       public int Past;
    }
  public National ValueForNational;
}

myclass.myvariable.National - it is a type (will not compile)

myclass.myvariable.National.Past - it is a member of a type (will not compile)

myclass.myvariable.ValueForNational - is value for a member of MyVariable member inside MyClass' variable (would compile)

myclass.myvariable.ValueForNational.Past - is one that you probably want...

Solution 3

Your "question" is not really a question at all, but here is a simple set of classes to acccomplish that. I used classes instead of structs as there are some "gotchas" you will need to understand if you use structs.

Unless maybe if you describe "it is not working", I bet that your problem actually is that you are trying to use structs.

Please post your code, and describe your symptoms to enable us to help you better.

public class Foo<T>
{
    public T Future;
    public T Present;
    public T Past;
}

public class Bar
{
    public Foo<string> City;
    public Foo<string> State;
    public Foo<string> Country;
}

public class Top
{
    public Bar v0;
    public Bar v1;
    //...
    public Bar v2;
}
Share:
17,320
Farhan
Author by

Farhan

Trying to learn what I have not learnt. SOreadytohelp

Updated on June 09, 2022

Comments

  • Farhan
    Farhan almost 2 years

    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.

  • cariseldon
    cariseldon almost 12 years
    Can you post code for "Now when I instantiate the class and access the member (myclass.myvariable.National.Past), it does not get the member."
  • Farhan
    Farhan almost 12 years
    I'm doing it like this: MyClass myclass = new MyClass();
  • cariseldon
    cariseldon almost 12 years
    If that is the entirety of your code, you've done nothing at all except instantiate the class. How are you setting the values? How are you retrieving the values? What are you seeing? What are you expecting to see? We are not psychic.