I usually take the wimp's way out and use an ArrayList when I don't know the
array length in advance. Your code would look something like this:
int i = 0;
ArrayList debit = new ArrayList();
ArrayList credit = new ArrayList();
while (myRead3.Read())
{
debit.Add(myRead3.GetValue(0).ToString());
credit.Add(myRead3.GetValue(1).ToString());
i++;
}
You could also get rid of the int i = 0; and the i++; lines and just use the
foreach statement to iterate over the entire ArrayList collection, or use a
for loop that goes for (int i = 0; i < debit.Count; i++) if you need to keep
a count of the # of elements (the .Count property tells how many items you
have in the ArrayList, and elements are numbered from 0 up).
The downside, as far as I can tell, is that the ArrayList contains all
Objects. So it could use up more resources than other solutions, and you
may have to use the .ToString() method, or cast the elements back to
whatever type you are using when you retrieve them (it stores everything as
Objects, but remembers the type you used to store them; so it won't let you
perform an invalid cast on the ArrayList elements):
for (int i = 0; i < debit.Count; i++)
Console.WriteLine (debit
.ToString());
Please excuse any typos, it's late.
Thanks,
Michael C., MCDBA
Sorin Sandu said:
Is it posible to modify the lenghth of an array ?
I want to do something like
int i = 0;
string[] debit = new string[y];
string [] credit = new string[y];
while (myread3.Read())
{
debit = myread3.GetValue(0).ToString();
credit = myread3.GetValue(1).ToString();
i++;
}
But I don't know the value of y and I want to change it in the while loop.
How can I do that ?