469,946 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,946 developers. It's quick & easy.

ArrayList in a struct

Is it possible to use an ArrayList inside a struct? I keep running into a
null reference exception when I try to Add to the ArrayList in the struct,
and it won't let me initialize the ArrayList in the struct. Any ideas?

Thanks,
Michael C.
Nov 16 '05 #1
6 11775
Michael,

Guess, its not possible to keep ArrayList inside a struct. Its becoz

1.struct is a value type and ArrayList is a class which is a reference type
2. You can have struct inside a class, but not the reverse.

In this case, you are trying to keep a class object (reference object)
inside the struct (value type). This will throw an exception.

Shak.
"Michael C" <mi*******@optonline.net> wrote in message
news:Pj********************@news4.srv.hcvlny.cv.ne t...
Is it possible to use an ArrayList inside a struct? I keep running into a
null reference exception when I try to Add to the ArrayList in the struct,
and it won't let me initialize the ArrayList in the struct. Any ideas?

Thanks,
Michael C.

Nov 16 '05 #2
Michael,

Guess, its not possible to keep ArrayList inside a struct. Its becoz

1.struct is a value type and ArrayList is a class which is a reference type
2. You can have struct inside a class, but not the reverse.

In this case, you are trying to keep a class object (reference object)
inside the struct (value type). This will throw an exception.

Shak.
"Michael C" <mi*******@optonline.net> wrote in message
news:Pj********************@news4.srv.hcvlny.cv.ne t...
Is it possible to use an ArrayList inside a struct? I keep running into a
null reference exception when I try to Add to the ArrayList in the struct,
and it won't let me initialize the ArrayList in the struct. Any ideas?

Thanks,
Michael C.

Nov 16 '05 #3
Michael,
Is it possible to use an ArrayList inside a struct?
Yes. eg :

public struct SR
{
public int kk;
public ArrayList list; // XXX
}

private void button1_Click(object sender, System.EventArgs e)
{
SR sr = new SR();
sr.list = new ArrayList();
sr.list.Add("this should work");
MessageBox.Show(sr.list[0].ToString());
}

Limitations :
- you can't initialize the ArrayList at XXX. eg. public ArrayList list =
new ArrayList() won't compile.
- can't create your own parameterless constructors for structs. Even if you
create a constructor
with parameters and initialize the ArrayList there, you can't stop the user
from using the
default (parameterless) constructor, since there's no way of overriding it.

HTH,
Stephen
"Michael C" <mi*******@optonline.net> wrote in message
news:Pj********************@news4.srv.hcvlny.cv.ne t... Is it possible to use an ArrayList inside a struct? I keep running into a
null reference exception when I try to Add to the ArrayList in the struct,
and it won't let me initialize the ArrayList in the struct. Any ideas?

Thanks,
Michael C.

Nov 16 '05 #4
Michael,
Is it possible to use an ArrayList inside a struct?
Yes. eg :

public struct SR
{
public int kk;
public ArrayList list; // XXX
}

private void button1_Click(object sender, System.EventArgs e)
{
SR sr = new SR();
sr.list = new ArrayList();
sr.list.Add("this should work");
MessageBox.Show(sr.list[0].ToString());
}

Limitations :
- you can't initialize the ArrayList at XXX. eg. public ArrayList list =
new ArrayList() won't compile.
- can't create your own parameterless constructors for structs. Even if you
create a constructor
with parameters and initialize the ArrayList there, you can't stop the user
from using the
default (parameterless) constructor, since there's no way of overriding it.

HTH,
Stephen
"Michael C" <mi*******@optonline.net> wrote in message
news:Pj********************@news4.srv.hcvlny.cv.ne t... Is it possible to use an ArrayList inside a struct? I keep running into a
null reference exception when I try to Add to the ArrayList in the struct,
and it won't let me initialize the ArrayList in the struct. Any ideas?

Thanks,
Michael C.

Nov 16 '05 #5
Shakir Hussain <sh**@fakedomain.com> wrote:
Guess, its not possible to keep ArrayList inside a struct. Its becoz
Yes it is.
1.struct is a value type and ArrayList is a class which is a reference type
2. You can have struct inside a class, but not the reverse.
Yes you can.
In this case, you are trying to keep a class object (reference object)
inside the struct (value type). This will throw an exception.


No it won't.

Here's an example program.

using System;
using System.Collections;

struct Container
{
ArrayList list;

public Container(params object[] members)
{
list = new ArrayList();
foreach (object o in members)
{
list.Add(o);
}
}

public Container (Container container)
{
this.list = container.list;
}

public void Display()
{
foreach (object o in list)
{
Console.WriteLine (o);
}
}
}

class Test
{
static void Main(string[] args)
{
Container c = new Container(args);
Container other = new Container(c);
other.Display();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Shakir Hussain <sh**@fakedomain.com> wrote:
Guess, its not possible to keep ArrayList inside a struct. Its becoz
Yes it is.
1.struct is a value type and ArrayList is a class which is a reference type
2. You can have struct inside a class, but not the reverse.
Yes you can.
In this case, you are trying to keep a class object (reference object)
inside the struct (value type). This will throw an exception.


No it won't.

Here's an example program.

using System;
using System.Collections;

struct Container
{
ArrayList list;

public Container(params object[] members)
{
list = new ArrayList();
foreach (object o in members)
{
list.Add(o);
}
}

public Container (Container container)
{
this.list = container.list;
}

public void Display()
{
foreach (object o in list)
{
Console.WriteLine (o);
}
}
}

class Test
{
static void Main(string[] args)
{
Container c = new Container(args);
Container other = new Container(c);
other.Display();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Michael C | last post: by
1 post views Thread by Peter Schmitz | last post: by
8 posts views Thread by Maileen | last post: by
31 posts views Thread by Extremest | last post: by
3 posts views Thread by pengbsam | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.