eitanb

Hello,

I have a structure defined as the follows:

[Serializable]

public struct myProgram

{

public MyHeader myHeader;

// Variable Section: Current Segment List Declaration

public BindingList<mySegment> listSegments ;

}

I used new to create an object:

public static myProgram currentProgram = new myProgram();

I am not sure if I have to create an object using another new in this for listSegments int this form:

public static BindList<mySegment> listSegments = new BindingList<mySegment>();

or the first new operator (for myProgram) already took care of that

Thanks

Eitan



Re: Visual C# Language using the "new" to create structures

OmegaMan

No, if you try to access it you will get an invalid object exception. Yes myProgram has been created but it has a reference named listSegments which starts out life as a null reference. Either new up the list before its first use, or do the new at the declaration line. Then the reference named listSegements will refer to an actual object and can be used.





Re: Visual C# Language using the "new" to create structures

eitanb

Hello,

should I create an object (using new) also for MyHeader (my header is a simple structure) Some thing like:

public static myProgram currentProgram = new myProgram();

and then:

currentProgram.myHeader = new MyHeader() ;

Or this is only required for the BindingList declaration

Thank you,

Eitan





Re: Visual C# Language using the "new" to create structures

OmegaMan

I don't think I am following you...the static is not necessary unless the object is exposing it as a static resource. As to when you new up an object, if it resides on a class one can do this

class myclass
{
List<string> myList = new List<string>();
}

No static is required and a new list will be created when someone creates the class such as

myclass mc = new myclass(); // myList is ready to go.

or one can do it like this

class myclass
{
List<string> myList;

public myclass() // Setting it up on the constructor.
{
myList = new List<string>();
}

}

The point being...just make sure it is created before its first use within the class. Does that help







Re: Visual C# Language using the "new" to create structures

eitanb

Hello OM,

Thanks

Eitan