Custom class array syntax help

  • Thread starter Thread starter Mick
  • Start date Start date
M

Mick

Hello, I know C++ but am new to C# so bear with me.

I want to create an array of my own class. Something like this:
///////////////////////////////////////
class CLoginInfo
{
public string name;
public string id;
public string pw;

public CLoginInfo()//constructor
{
this.name = "";//initialize
this.id = "";
this.pw = "";
}
}
private CLoginInfo[] m_logins;

public Form1()
{
m_logins = new CLoginInfo[3];
...
m_logins[0].name = "Mick";//???? exception!!!
m_logins[0].id = "MicksID";
m_logins[0].pw = "MicksPW";
...
m_logins[1].name = "Joe";
m_logins[1].id = "JoesID";
m_logins[1].pw = "JoesPW";
...
}
///////////////////////////////////////

This compiles but I get the runtime exception error: "Object reference
not set to an instance of an object."

What is the proper syntax?

Also is there a way to dynamically change the size of the array?

Thank you for any suggestions,
Mick
 
By doing m_logins = new CLoginInfo[3];
You actually declare an array that refers to CLoginInfo instances. You do
not create the objects.
After your creation, if you look at the content of m_logins[0], it will be
null.

You habe to create the 3 objects and assign them to your array.

Joaé
 
So would I have to do this in a "For" loop in the runtime area? Is this
the standard C# way to use custom arrays? Or am I going about this
whole thing wrong?
 
Back
Top