开发者

Access Object in Form Class

A fo开发者_开发技巧llow-up question to one earlier: I've created an object in the Form1 : Form class with:

public struct POStruct
    {
        public List<string> staticCustInfo;
        public List<List<string>> itemCollection;
        public int testInt;
    }

    POStruct myObject = new POStruct();

However, when I try to access myObject from public void ItemSubmit_Click_1(object sender, EventArgs e) I get errors saying it's not instantiated. I thought I already instantiated it above with the line POStruct myObject = new POStruct();?

Thanks for the help.


It's likely that you are using one of the members of the struct without first initializing it. Structs cannot actually be null anyway, but their members can.

In other words, myObject is not null, and cannot actually be null since it is a variable of a struct type. But from your question, it sounds like myObject.staticCustInfo and myObject.itemCollection are.

But without seeing the exact code that is triggering the exception, all I can do is guess.


You're instantiating POStruct, but that struct has two List<> objects that aren't instantiated. When you instantiate myObject, you need to set these two properties to something of use, e.g.:

POStruct myObject = new POStruct();
myObject.StaticCustInfo = new List<string>();
myObject.itemCollection = new List<List<string>>();

If that doesn't help, can you post the whole class, with event handler, the exact message, and the line of code that's triggering the exception?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜