Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C# Arraylist with class

Status
Not open for further replies.

islouis

Junior Member level 1
Junior Member level 1
Joined
Sep 24, 2004
Messages
17
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
194
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
 

counting multiples in a c# arraylist

Hi,
i have fixed the issues in your bug and left for you to analyze what i have modified, analyze it and try to think why this was done.

using System;
using System.Collections;

class contact
{
public string Name
{
get
{
return name;
}

set
{
name=value;
}
}
string name;
public string Status
{
get
{
return status;
}
set
{
status=value;
}
}
string status;

public void Setdate ( string status, string name)
{
this.status = status;
this.name = name;
}
}
class entry
{
public static void Main()
{
contact obj1 = new contact();
contact obj2 = new contact();

ArrayList container= new ArrayList(101);


obj1.Name = "John";
obj1.Status = "Online";


container.Add(obj1); // I want to pass it into ArrayList, but exception

obj2.Name = "Peter";
obj2.Status = "OFFLINE"; // Now i create another obj and want to store into the ArrayList

container.Add(obj2); // so now should have multiple object contain inside the ArrayList?

for (int i=0; i<container.Count; i++)
Console.WriteLine("Name = {0} Status = {1} ", ((contact)container).Name,((contact)container).Status); // How do i print it out?

}
}
 

    islouis

    Points: 2
    Helpful Answer Positive Rating
c# container.add

Thanks a lot!!!!
I think i know what's wrong with my version now! thank!

Louis
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top