Array Size

J

Jim Heavey

Do I have to know the exact number of occurences of an array object when I
declared it, or can I add just declare the object and do not have to
declare the length?

For instance...

TableRow[] myTableRows;

Table row1 = new TableRow();
myTableRows[0] = row1;
Table row2 = new TableRow();
myTableRows[1] = row2;

I think I had to know the size of my array - at least in Java I did, so I
guess it is the same with C#?

If I do not know the size exactly, is it best for me to choose a number
that I know it will not exceed (small number like 10) or should I just use
a hashtable to store all of my row in? Or is there a better object ot use?
 
S

Scott Mitchell [MVP]

To resize an array you essentially need to create a new array of the
proper size and copy over the elements of the old array.

If you need dynamic add/remove capabilities, you might want to look into
the ArrayList. It does appropriate resizing for you under the covers.

For more info on arrays and ArrayLists, check out:

An Extensive Examination of Data Structures: Part 1
http://msdn.microsoft.com/vcsharp/d...us/dv_vstechart/html/datastructures_guide.asp

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
D

David Jessee

Here's another option. If you don't know exaclt how many items you're going
to end up with, you can start with an arrayList collection and then, when
you're done, turn it into as array if neded.

ArrayList myTableRows=new ArrayList();
Table row1 = new TableRow();
myTableRows.Add(row1);
Table row2 = new TableRow();
myTableRows.Add(row2);
TableRow[] theTableRowArray;
theTableRowArray=myTableRows.ToArray(TableRow);
 

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

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top