Equivalent of Redim

  • Thread starter Thread starter san
  • Start date Start date
san said:
Wht is equivalent of Redim(VB) in C#.

Create a new array of the right size, use Array.Copy to copy to give
the new array the same contents (where appropriate) as the old array,
and then discard the old array, using the new one instead.

Alternatively, use ArrayList from the start, where appropriate.
 
I frequently use ArrayList internally, then copy to a strongly typed array
before returning.

private string[] DeDup(string[] Source)
{
ArrayList ret = new ArrayList();
string[] dest = null;

for (int i = 0; i < Source.Length; i++)
{
if (!ret.Contains(Source)) //prevent duplicate entries
ret.Add(Source);
}

if (ret.Count > 0)
{
dest = new string[ret.Count];
ret.CopyTo(dest);
}

return dest;
}

HTH,
Eric Cadwell
http://www.origincontrols.com
 
If you are doing this frequently, why then don't you use a strongly typed
collection instead?
This way, you have the convenience of ArrayList without the penalty of the
Copy at the end.
Just find a strongly typed collection generator (there are many) and point
it at your type (in this case, string)
Just a thought,

Oscar.
 
Eric Cadwell said:
I frequently use ArrayList internally, then copy to a strongly typed array
before returning.

<snip>

Rather than do the copying manually, you can use
ArrayList.ToArray(typeof(string)) instead.
 
Back
Top