Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Using a List<> for storage

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hoop - 25 Feb 2008 14:38 GMT
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
Jon Skeet [C# MVP] - 25 Feb 2008 14:43 GMT
<snip>

> 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
Hoop - 25 Feb 2008 17:42 GMT
> <snip>
>
[quoted text clipped - 14 lines]
>
> 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
Jon Skeet [C# MVP] - 25 Feb 2008 18:14 GMT
<snip>

> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.