How to set dimension of arrays inside nested structs

73 views Asked by At

I have to use a structure containing a structure that contains an array of a structure. When I try to fix a dimension for the array the compiler (csc.exe) returns me an error:

Blockquote error CS0426 The type name 'strInner_1' does not exist in the type 'MyApp.strMAIN'

This is my code:

public class MyApp
{

    public struct strInner_2
    {
        public int Num;
    }

    public struct strInner_1
    {
        public string month_name;
        public strInner_2[] number;
        public int days_num;
    }
    
    public struct strMAIN
    {
        public strInner_1 Main_1;
        public strInner_1 Main_2;
    }
    
    static void Main()
    {
        
        
        strMAIN.strInner_1.Main_1.number[] Numbers_1 = new strMAIN.strInner_1.Main_1.number[12];
        strMAIN.strInner_1.Main_2.number[] Numbers_2 = new strMAIN.strInner_1.Main_2.number[12];
    }
}

what am I doing wrong?

Thanks

1

There are 1 answers

0
cyberjujutsu On BEST ANSWER

It appears that you're trying to define arrays of the strInner_2 structure within the strInner_1 structure inside the strMAIN structure. However, the way you are trying to access these structures and create arrays is incorrect. In C#, you don't specify the structure name along with the array declaration. Instead, you should access the structures and arrays

public class MyApp
{
    public struct strInner_2
    {
        public int Num;
    }

    public struct strInner_1
    {
        public string month_name;
        public strInner_2[] number;
        public int days_num;
    }

    public struct strMAIN
    {
        public strInner_1 Main_1;
        public strInner_1 Main_2;
    }

    static void Main()
    {
        strMAIN myStruct = new strMAIN();
        myStruct.Main_1.number = new strInner_2[12];
        myStruct.Main_2.number = new strInner_2[12];

        // You can now access the arrays like this:
        myStruct.Main_1.number[0].Num = 42;
        myStruct.Main_2.number[0].Num = 24;
    }
}