2 D Array initialization error in Structure

koteswara rao

Member
Joined
Feb 1, 2022
Messages
12
Programming Experience
3-5
My Code is divided as 3 parts
1.
C#:
    struct QUAD_DATA_TYPE
        {
            public  float min;
            public  float max;
            public  float center;
            public  float aoa_peak_pos;
            public  float aoa_peak_neg;
            public  float aoa_peak;
            public  float aoa_rms;
            public  List<float> aoa;
            public  List<float> angle;
            public  List<float> phase_A;
            public  List<float> phase_B;
            public  List<float> PD_A;
            public  List<float> PD_B;
            public  List<float> ratio;
            public  List<int> idx;
        }

         struct DATA_TYPE
         {

            public List<float> min;
            public List<float> max;
            public List<float> ave;
            public float largest_aoa_peak_pos;
            public float largest_aoa_peak_neg;
            public float largest_aoa_peak;
            public float largest_aoa_rms;
            public float overall_aoa_rms;
            public float overall_aoa_ave;
            public List<float> angle;
            public List<List<float>> element;
            public List<List<float>> phase;
            public QUAD_DATA_TYPE[] quad;
    }
2.
C#:
DATATYPE ant;
3.
C#:
                        ant.element[0][I] = 1.0f;
                        ant.element[1][I] = (float)Convert.ToDouble(SplitSpaceListDataOfAntinnafile[2]);
                        ant.element[2][I] = (float)Convert.ToDouble(SplitSpaceListDataOfAntinnafile[3]);
                        ant.element[3][I] = (float)Convert.ToDouble(SplitSpaceListDataOfAntinnafile[4]);[/I][/I][/I][/I]


when i execute the code the run time error as
1672286771239.png




please find the solution.



regards
Koteswara rao M
 
Last edited by a moderator:
You can't write code like this:

C#:
List<List<float>> element;
element[0] = 1.0f;

The code you've posted isn't even the code that you've compiled. The thing you can store at element[0] is a List<float> not a float number so that code won't even run ("There is no conversion from float to List<float>")

C#:
List<List<float>> element;
element[0][0] = 1.0f;

That code will run, but crash because element has only been declared. It hasn't been set to an instance of a list. You need to make a new list before you can put something in it. Also you need to have an index 0 before you can set index 0, which means you need to add something to the list first:

C#:
List<List<float>> element;

element = new List<List<float>>();
element.Add(new List<float>());
element[0].Add(1.0f);

//this is now redundant
//element[0][0] = 1.0f;

Putting something as a struct does not mean that all its members automatically get created as instances you can use; you still have to use new. The most minimal code to do so would be to use a 2D array, not a list of lists. A 2D array looks like:

C#:
float[,] element = new float[4,3];

But the size is predetermined and cannot have more slots added to it on the fly. To increase the size of a 2D array you have to make a new one and copy all the info from the old one across

Also, those things you have declared to be structs really should be classes; they're too big
 
Last edited:
I tried whole of these things still the same error is coming.
instead of structure I tried separate class also like this still the same error is coming.
help me on this..

1672632830354.png



Regards
Koteswara rao M
 
Please post code in code tags, not as screenshots. To use code tags, use the toolbar icon that looks like </>
 
Please re-read post #2 more carefully. @cjard was very explicit on how to create and initialize a real C# 2 D array, as well as how to create a list of list of floats.
 
The code I put in post 2 isn't something you can paste into your code and make your code work

It is intended to teach you that you can't just declare a variable that is allowed to store a list, but not actually make a list to go in it, then start adding things to the list

Your List<float> angle; needs to be List<float> angle = new List<float>();

I recommend you stay away from List<List<float>> until you've understood what is going on with a single dimension list

I also recommend that you don't use structs here, and that you name your public fields in PascalCase
 
The code I put in post 2 isn't something you can paste into your code and make your code work

It is intended to teach you that you can't just declare a variable that is allowed to store a list, but not actually make a list to go in it, then start adding things to the list

Your List<float> angle; needs to be List<float> angle = new List<float>();

I recommend you stay away from List<List<float>> until you've understood what is going on with a single dimension list

I also recommend that you don't use structs here, and that you name your public fields in PascalCase

i tried the below code

Method 3:
float[,] element = new float[4,3];


its working

Thanks and Regards
Koteswara rao M
 
Back
Top Bottom