473,383 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 12131
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael C | last post by:
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...
1
by: Peter Schmitz | last post by:
Hi, I've got the following problem: I need to create a special Dll that is based on MFC, but also includes also managed code (which works fine and isn't a problem). The problem occurrs in the...
8
by: Maileen | last post by:
Hi, I would like to have something like an array (something like a collection) in order to store the following things : company name1, new1, old1, in1, out1 company name2, new2, old2, in2,...
3
by: Frank H. | last post by:
Hi, in my webservice I have a Webmethod wich returns a list of Objects to my C++ Application. C# code: public class Contact { public string FirstName;
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
3
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code in C#: In global--> public struct point { int x; string y; }
2
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code: In global--> public struct point { int x; string y; } static public point point;
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
4
by: bbg9507 | last post by:
Hi, question about using ArrayList from C# newbie, I managed to make my array list which holds a number of structs: struct myStruct { public int field1; public int field2; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.