Redim a String[]

  • Thread starter Thread starter Raymond Du
  • Start date Start date
R

Raymond Du

Hi,

Is it possible to redim a String[]? Say I have the following code:

String[] strs = {"a", "b", "c"};

How do I add more String into strs?

Thanks in Advance
 
George Prado said:
You cannot directly add to the array but if you declare a new array with more
open locations and clone the original you can then place new values into that
array as in the following:

string[] strs = {"a", "b", "c"};
string[] arrCollection = (string[])strs.Clone();
strs = new string[arrCollection.Length + 1];
arrCollection.CopyTo(strs,0);
strs[strs.Length - 1] = "new value";

if you look at the IEnumerable interface you might be able to create a
string collection object that you can place that kind of code into a method
of the collection. Information on that is located on the MSDN website at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkForeachTutorial.asp

With a collection class, you can reuse the code for your method to add a new
member to an array.

George Prado

Raymond Du said:
Hi,

Is it possible to redim a String[]? Say I have the following code:

String[] strs = {"a", "b", "c"};

How do I add more String into strs?

Thanks in Advance

Have you tried the ArrayList collection out? Very similar to an array, but has
an Add method :)

Hope it helps!

Mythran
 
I have not tried that option yet! I will definatly look at it and thanks for
the insight. I am still learning .Net as most I'm sure are :) thanks again.

George Prado

Mythran said:
George Prado said:
You cannot directly add to the array but if you declare a new array with more
open locations and clone the original you can then place new values into that
array as in the following:

string[] strs = {"a", "b", "c"};
string[] arrCollection = (string[])strs.Clone();
strs = new string[arrCollection.Length + 1];
arrCollection.CopyTo(strs,0);
strs[strs.Length - 1] = "new value";

if you look at the IEnumerable interface you might be able to create a
string collection object that you can place that kind of code into a method
of the collection. Information on that is located on the MSDN website at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkForeachTutorial.asp

With a collection class, you can reuse the code for your method to add a new
member to an array.

George Prado

Raymond Du said:
Hi,

Is it possible to redim a String[]? Say I have the following code:

String[] strs = {"a", "b", "c"};

How do I add more String into strs?

Thanks in Advance

Have you tried the ArrayList collection out? Very similar to an array, but has
an Add method :)

Hope it helps!

Mythran
 
You can also use System.Text.StringBuilder, it includes an append function.

Regards,
John

George Prado said:
I have not tried that option yet! I will definatly look at it and thanks for
the insight. I am still learning .Net as most I'm sure are :) thanks again.

George Prado

Mythran said:
George Prado said:
You cannot directly add to the array but if you declare a new array with more
open locations and clone the original you can then place new values into that
array as in the following:

string[] strs = {"a", "b", "c"};
string[] arrCollection = (string[])strs.Clone();
strs = new string[arrCollection.Length + 1];
arrCollection.CopyTo(strs,0);
strs[strs.Length - 1] = "new value";

if you look at the IEnumerable interface you might be able to create a
string collection object that you can place that kind of code into a method
of the collection. Information on that is located on the MSDN website at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkForeachTutorial.asp
With a collection class, you can reuse the code for your method to add a new
member to an array.

George Prado

:

Hi,

Is it possible to redim a String[]? Say I have the following code:

String[] strs = {"a", "b", "c"};

How do I add more String into strs?

Thanks in Advance

Have you tried the ArrayList collection out? Very similar to an array, but has
an Add method :)

Hope it helps!

Mythran
 
The easiest method that I've found is to use a System.Collections.ArrayList,
then do it like this:

ArrayList a = new ArrayList();
a.Add("abc");
a.Add("def");
//....etc....

string[] strs = (string[])a.ToArray(typeof(string));
 
George Prado said:
You cannot directly add to the array but if you declare a new array with more
open locations and clone the original you can then place new values into that
array as in the following:

string[] strs = {"a", "b", "c"};
string[] arrCollection = (string[])strs.Clone();
strs = new string[arrCollection.Length + 1];
arrCollection.CopyTo(strs,0);
strs[strs.Length - 1] = "new value";

Why are you calling Clone in the first place here? What's the point of
creating *two* new arrays rather than just one?
 
It's possible but not the recommended. Use an ArrayList instead for
resizable collections.
 
Many people seem to be 'stuck' on ArrayList, but if you know that
you're dealing with strings then a StringCollection will avoid
boxing/unboxing issues and you'll also know that the only thing
you'll have in the StringCollection is strings - each element is a
string - no 'ToString' or conversion or unboxing.
 
David Anton said:
Many people seem to be 'stuck' on ArrayList, but if you know that
you're dealing with strings then a StringCollection will avoid
boxing/unboxing issues and you'll also know that the only thing
you'll have in the StringCollection is strings - each element is a
string - no 'ToString' or conversion or unboxing.

Never dealt with the StringCollection before. Maybe because I haven't jumped
into C# for applications yet (had to learn C# though, so I know it...but just
haven't run into the ReDim situation in C#).

The StringCollection is a better way to go instead of the ArrayList.


Mythran
 
Strings aren't value types, so there isn't any boxing. It would save you the
casting though.

....and I don't think calling ToString on a string is really a conversion. ;)

Etienne Boucher
 
David,
Strings are reference types. No Boxing & Unboxing involved!

However you are correct you would need to cast (or use ToString) the value
from the indexer of ArrayList from Object to String.

You are correct StringCollection would avoid this cast from Object to
String.

Hope this helps
Jay
 
Jay B. Harlow [MVP - Outlwrote:
David,
Strings are reference types. No Boxing & Unboxing involved!

However you are correct you would need to cast (or use ToString) the value
from the indexer of ArrayList from Object to String.

You are correct StringCollection would avoid this cast from Object to
String.

Hope this helps
Jay



"David Anton"
in message Many people seem to be 'stuck' on ArrayList, but if you know that
you're dealing with strings then a StringCollection will avoid
boxing/unboxing issues and you'll also know that the only thing
you'll have in the StringCollection is strings - each element is a
string - no 'ToString' or conversion or unboxing.
[/quote:4b7bcd2f7a]

Of course.. :oops:

Agreed - it's just the casting you save (and the fact that you don't
have to be concerned about other types being in the list).
 
Jay B. Harlow [MVP - Outlwrote:
David,
Strings are reference types. No Boxing & Unboxing involved!

However you are correct you would need to cast (or use ToString) the value
from the indexer of ArrayList from Object to String.

You are correct StringCollection would avoid this cast from Object to
String.

Hope this helps
Jay



"David Anton"
in message Many people seem to be 'stuck' on ArrayList, but if you know that
you're dealing with strings then a StringCollection will avoid
boxing/unboxing issues and you'll also know that the only thing
you'll have in the StringCollection is strings - each element is a
string - no 'ToString' or conversion or unboxing.
[/quote:81561461db]

Of course.. :oops:

Agreed - it's just the casting you save (and the fact that you don't
have to be concerned about other types being in the list).
 
Etienne Boucherwrote:
Strings aren't value types, so there isn't any boxing. It would save
you the
casting though.

....and I don't think calling ToString on a string is really a conversion. ;)

Etienne Boucher

You're right about the boxing (I've already fessed up to my error).

But the "ToString" is required - an ArrayList element is not
necessarily a string. And it is a conversion.
 
David Anton said:
You're right about the boxing (I've already fessed up to my error).

But the "ToString" is required - an ArrayList element is not
necessarily a string. And it is a conversion.

ToString() is not a conversion, it's a method call. However, you don't
need to call ToString() if you just cast. (That's a conversion of
reference type, but not of actual object.)

I would never (or at least, almost never) call ToString() on ArrayList
items to get them as strings - if I think they're already strings, I'll
just cast them to string and get an exception if I turn out to be
wrong. If I know some may not be strings, I'm unlikely to rely on
ToString() doing the right thing. It's only in the rare case where
they'll all be instances of my own class which definitely overrides
ToString() in an appropriate way that I'll call ToString() on them.
 
Back
Top