简体   繁体   中英

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.

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.

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;
}

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...

there is a initialization missing under struct MyVaribale

public National national;

internal class Program
    {
        static void Main(string[] args)
        {
            var myVar = new MyClass();
            myVar.myvariable.national.Past = 1;
        }
    }
    
    public class MyClass
    {
        public MyVariable myvariable;
    }

    public struct MyVariable
    {
        public National national;  // This line is missing thats why You cant access the members of National struct.

        public struct National
        {
            public int Past;
            public int Present;
            public int Future;
        }
    
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM