0

I have a class

namespace MyClassNameSpace {
   public struct a {
       public float Time;
       public float High;
   }
   public class SoC {
       public string Name;
       public a[] Stock =new a[9];
   }

}

And in Form1 I create an array of SoC:

public static SoC[] b = new SoC[5];
private void Form1_KeyUp(object sender, KeyEventArgs e) {
    b[1].Name = "ABC"
}

The problem is that b is an array of nulls instead of being an array of empty (zeroed) SoC... How do I change that?

2 Answers 2

9

You are creating an array of reference variables of Reference (class) type. The statement,

public static SoC[] b = new SoC[5];

creates 5 reference variables whose initial value is null.

This way you can create objects:

for(int i=0;i<b.Length;i++) {
    b[i]=new SoC();
}
Sign up to request clarification or add additional context in comments.

Comments

3

Allocating space for the array doesn't actually create the objects. You need to loop through and call the constructor for each item.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.