Using a List<> for storage

H

Hoop

Hi,
I have a project where I have to store what we call modules. To get
the project going I have been using an array, however this will not be
the best way as the items that will be stored are dynamic, might or
might not be an instance of one.
What I have is a abstract base class where I will create instances of
as needed, here is the base class.

public abstract class BaseModule
{
//circuit names
private List<string> circuitNameList = new List<string>();
//IO point numbers
private List<string> ioPointNumbersList = new List<string>();
//circuit numbers
private List<string> circuitNumberList = new List<string>();
//circuit type, as in Input or Output
private List<string> circuitTypeList = new List<string>();
//point type, as in Digital, ground, etc.
private List<string> ioPointTypeList = new List<string>();

//access to the circuit name list
public List<string> CircuitNames
{
get { return circuitNameList; }
set { circuitNameList = value; }
}

//access to the io point numbers list
public List<string> IOPointNumbers
{
get { return ioPointNumbersList; }
set { ioPointNumbersList = value; }
}

//access to the circuit numbers list
public List<string> CircuitNumber
{
get { return circuitNumberList; }
set { circuitNumberList = value; }
}

//access to the circuit type list
public List<string> CircuitType
{
get { return circuitTypeList; }
set { circuitTypeList = value; }
}

//access to the iopoint type list
public List<string> IOPointType
{
get { return ioPointTypeList; }
set { ioPointTypeList = value; }
}

//how many IO's
//use circuit name count
public int RecordCount()
{
return circuitNameList.Count;
}

}

For one series of modules that I am testing there can be 0 - 16 of
these, currently I am using an array like so,
public TestModules[] testModule = new TestModules[16];

These testModules inherit from the above base class as is for now.
Problem here is that there might not be any of these modules, and if
there are some the index will not nessasarily be in order, could be
module 0, 6, 9, and order. The index ID's the module. Each module will
have 16 pieces of the data listed in the baseclass.
I was thinking of createing a list for these,

private List<TestModules> testModuleslist = new List<TestModules>();

When I add a piece of data, each single added piece of data moves up
the list, like so

aMod.CircuitNames.Add(this.circuitNameTxtBox.Text);
aMod.IOPointNumbers.Add(this.IOPointNumberTxtBox.Text);
aMod.CircuitNumber.Add(this.circuitNumberTxtBox.Text);
aMod.CircuitType.Add(this.cmbCircuitType.Text);
aMod.IOPointType.Add(this.cmbIOPointType.Text);

I wind up with the data added to the list in the above order, but I
cannot correctly id to what module it belongs
I am trying to get something like,

aMod[1]CircuitNames[1].Add(this.circuitNameTxtBox.Text);
aMod[1].IOPointNumbers[1].Add(this.IOPointNumberTxtBox.Text);
aMod[1].CircuitNumber[1].Add(this.circuitNumberTxtBox.Text);
aMod[1].CircuitType[1].Add(this.cmbCircuitType.Text);
aMod[1].IOPointType[1].Add(this.cmbIOPointType.Text);

aMod[1]CircuitNames[2].Add(this.circuitNameTxtBox.Text);
aMod[1].IOPointNumbers[2].Add(this.IOPointNumberTxtBox.Text);
aMod[1].CircuitNumber[2].Add(this.circuitNumberTxtBox.Text);
aMod[1].CircuitType[2].Add(this.cmbCircuitType.Text);
aMod[1].IOPointType[2].Add(this.cmbIOPointType.Text);

the above could go on to where the second idex gets up 15, this would
be one module.
the first index, aMod[1], also could go up to 16, aMod[2], aMod[3],
etc, with 16 pieces of the point data
for each mod[].

Then when I access the data I can find the specific points.
I am not certian if I can even achieve this with a List<>.
Hope I posted enough code and description for a good example.
Any storage suggesstions would be appreciated.
Thanks
Jeff
 
J

Jon Skeet [C# MVP]

Then when I access the data I can find the specific points.
I am not certian if I can even achieve this with a List<>.
Hope I posted enough code and description for a good example.
Any storage suggesstions would be appreciated.

I'm afraid I'm not really sure I understand your data model. However,
if you want to be able to simply go from an integer to a particular
module (without adding them sequentially), to options suggest
themselves:

1) Prepopulate the list with 16 null entries, then replace the value
when you create a new module, rather than calling Add

2) Use a Dictionary<int,Module> instead

Jon
 
H

Hoop

I'm afraid I'm not really sure I understand your data model. However,
if you want to be able to simply go from an integer to a particular
module (without adding them sequentially), to options suggest
themselves:

1) Prepopulate the list with 16 null entries, then replace the value
when you create a new module, rather than calling Add

2) Use a Dictionary<int,Module> instead

Jon

Hi Jon,
Yes, I was afraid my explanation was not very good.
Rather than saying where I want to go, I will just try to explain what
it looks like now.
In using arrays I allocate for any instance of a module, in this case
there can be 16 of them, so I would
just have an arrray of the 16 whether they will really exist or not.
for (int modIndex = 0; ccIndex < 16; modIndex++)
{
aModule[modIndex] = new Modules();
}

Each instance of the module can contain those data lists from my
previous post,
which is new, used to be arrays within aModule, now I changed to
these lists,
private List<string> circuitNameList = new List<string>();

So there can be within each of these modules, 16 circuitNames in this
list.
So if I want to access the first circuitName, from say module 1,
aModule[1].circuitName[0];
for the second circuitName,
aModule[1].circuitName[1];
.....
aModule[1].circuitName[15];

So each amodule[0..15] has arrays of data[0 ..15] within them.
I am trying to make these dynamic. I did do something similar a few
years ago using ArrayList.
Thanks
Jeff
 
J

Jon Skeet [C# MVP]

So each amodule[0..15] has arrays of data[0 ..15] within them.
I am trying to make these dynamic. I did do something similar a few
years ago using ArrayList.

Well, as I say - having List<T> instances with appropriate pre-
allocated empty values should be fine.
 

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