Add objects in arraylist

A

Arjen

Hi,

public class test {
public ArrayList myList = new ArrayList();
}

test myTest = new test();
myTest.myList.Add(<someobject>);

This results in an error.

I also tried to add objects inside a new arraylist.
And then set the new arraylist to the object arraylist.
test.myList = newarraylist;

This also results in an error.

How to add objects to this arraylist?

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Arjen,

You aren't being too specific about what the error is, or how you are
getting it.

Can you show some code? An arraylist should take anything that derives
from Object (meaning, anything) in the Add method.
 
G

Guest

Arjen
here is a "Short but Complete" (a-la Jon Skeet) example that works fine for
me:

using System;
using System.Collections;
using System.Data;
namespace ArrayListTest
{
class TestMe
{
[STAThread]
static void Main(string[] args)
{
test myTest = new test();
DataSet ds = new DataSet();
ds.DataSetName="TestDs";
myTest.myList.Add(ds);
DataSet ds2 = (DataSet)myTest.myList[0];
Console.WriteLine(ds2.DataSetName );
Console.ReadLine();
}
}
public class test
{
public ArrayList myList = new ArrayList();
}
}
 
A

Arjen

Whoops, when bug hunting I was looking at the wrong place.
the code was correct (what I was thinking)... I found the right place I
fixed the problem.

Thanks for your response!



Nicholas Paldino said:
Arjen,

You aren't being too specific about what the error is, or how you are
getting it.

Can you show some code? An arraylist should take anything that derives
from Object (meaning, anything) in the Add method.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Arjen said:
Hi,

public class test {
public ArrayList myList = new ArrayList();
}

test myTest = new test();
myTest.myList.Add(<someobject>);

This results in an error.

I also tried to add objects inside a new arraylist.
And then set the new arraylist to the object arraylist.
test.myList = newarraylist;

This also results in an error.

How to add objects to this arraylist?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top