islouis
Junior Member level 1
c# arraylist of class
Dear Sir:
Im new to C# and Im trying to assign multipe object into Arraylist and the code as follow:
using System;
using System.Collections;
class contact
{
public string Name
{
get
{
return name;
}
set
{
this.name=name; //should this be Name=value;?
}
}
string name;
public string Status
{
get
{
return status;
}
set
{
this.status=status;
}
}
string status;
public void Setdate ( string status, string name)
{
this.status = status;
this.name = name;
}
}
class entry
{
public static void Main()
{
contact obj = new contact();
ArrayList container= new ArrayList(100);
obj.Name = "John";
obj.Status = "Online";
container.Add(obj); // I want to pass it into ArrayList, but exception
obj.Name = "Peter";
obj.Status = "OFFLINE"; // Now i create another obj and want to store into the ArrayList
container.Add(obj); // so now should have multiple object contain inside the ArrayList?
for (int i=0; i<container.Count; i++)
Console.WriteLine("{0} ", container); // How do i print it out?
}
}
There is a lot of mistake in above code, but what i really want to do is trying to assgin multiple "contac" object into Arraylist. So, if there is any advice is wellcome.
thanks!
Louis
Dear Sir:
Im new to C# and Im trying to assign multipe object into Arraylist and the code as follow:
using System;
using System.Collections;
class contact
{
public string Name
{
get
{
return name;
}
set
{
this.name=name; //should this be Name=value;?
}
}
string name;
public string Status
{
get
{
return status;
}
set
{
this.status=status;
}
}
string status;
public void Setdate ( string status, string name)
{
this.status = status;
this.name = name;
}
}
class entry
{
public static void Main()
{
contact obj = new contact();
ArrayList container= new ArrayList(100);
obj.Name = "John";
obj.Status = "Online";
container.Add(obj); // I want to pass it into ArrayList, but exception
obj.Name = "Peter";
obj.Status = "OFFLINE"; // Now i create another obj and want to store into the ArrayList
container.Add(obj); // so now should have multiple object contain inside the ArrayList?
for (int i=0; i<container.Count; i++)
Console.WriteLine("{0} ", container); // How do i print it out?
}
}
There is a lot of mistake in above code, but what i really want to do is trying to assgin multiple "contac" object into Arraylist. So, if there is any advice is wellcome.
thanks!
Louis