Sam Youtsey
Sam Youtsey

Reputation: 884

Access Object in Form Class

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

Upvotes: 0

Views: 177

Answers (2)

James King
James King

Reputation: 6353

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?

Upvotes: 0

cdhowie
cdhowie

Reputation: 169488

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.

Upvotes: 1

Related Questions