By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,408 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,408 IT Pros & Developers. It's quick & easy.

Array Size

P: n/a
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?

Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You might be better off with ArrayList, which has Add/AddRange methods

HTH
Alex

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
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?

Nov 18 '05 #2

P: n/a
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/de...ures_guide.asp

Happy Programming!

--

Scott Mitchell
mi******@4guysfromrolla.com
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
Nov 18 '05 #3

P: n/a
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);

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
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?

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.