Add values to string array ?

P

Pim75

Hello,

I'm defining a string array like:
Dim strArray() As String = {"1", "2"}

Can I add some values to this string array later in the code?
It's not clear to me how to do this. I hope someone can help me.

Thanks in advance!
 
E

Eliyahu Goldin

No, you can't. A regular array has fixed size. You can use ArrayList or
other collection types for variable size.
 
P

Pim75

Thanks,
Would it be possible to give a little example of how to fill an
ArrayList and
convert it to a string Array after that?

I need the string Array to call another function.


No, you can't. A regular array has fixed size. You can use ArrayList or
other collection types for variable size.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin




I'm defining a string array like:
Dim strArray() As String = {"1", "2"}
Can I add some values to this string array later in the code?
It's not clear to me how to do this. I hope someone can help me.
Thanks in advance!- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -
 
D

David Longnecker

Here's a quick example. The presentation layer (ASPX page) simply has two
labels on it (lblArrayList and lblStringArray) to verify our output. The
ArrayList is part of the System.Collections namespace, so be sure to add
that in to your project.

// Create ArrayList object.
ArrayList myArrayList = new ArrayList();

// Add values; you can do this anywhere.
myArrayList.Add("Hello!");
myArrayList.Add("Goodbye!");

// Copy ArrayList to an object array.
object[] myStringArray = myArrayList.ToArray();

// Output ArrayList to our label to see the results.
foreach (string item in myArrayList)
{
lblArrayList.Text += item.ToString() + ", ";
}

// Output our array to our label to see the results.
foreach (string item in myStringArray)
{
lblStringArray.Text += item.ToString() + ", ";
}

---
David R. Longnecker
Web Developer
http://blog.tiredstudent.com
Thanks,
Would it be possible to give a little example of how to fill an
ArrayList and
convert it to a string Array after that?
I need the string Array to call another function.

No, you can't. A regular array has fixed size. You can use ArrayList
or other collection types for variable size.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldin

Hello,

I'm defining a string array like:
Dim strArray() As String = {"1", "2"}
Can I add some values to this string array later in the code? It's
not clear to me how to do this. I hope someone can help me.

Thanks in advance!- Tekst uit oorspronkelijk bericht niet weergeven
-
- Tekst uit oorspronkelijk bericht weergeven -
 
G

Guest

ReDim Preserve still works in VB.
If you're using 2005, then Array.Resize also works.
But if you're resizing numerous times you may run into performance issues
with either of these approaches since they copy the array on each resize.
Unless you need to use an array, List(Of String) is a better choice in 2005,
or StringCollection in 2003.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
 
K

Kris Lankford

You can not add more items to the array after you have initialized it. What
you could do is this:

C#
--------------------------------------
ArrayList arrStrings = new ArrayList()
arrStrings.Add("1");
arrStrings.Add("2");

//If you need to turn this into an Array after you could just use the
following code

string[] strArray = (string[])arrStrings.ToArray(typeof(string));

//this will le tyou add as many values as you want dynamically and then turn
it back into an array when you are ready
//You could also use the List<T> class instead of the ArrayList class.

I am not sure how this would translate in VB????

Kris
 
G

Guest

See my earlier post. You can certainly add more items to an array in both C#
and VB (using Array.Resize, and additionally ReDim Preserve in VB), but there
may be performance issues if this is done often. In 2005, List(Of String) is
a good alternative.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter


Kris Lankford said:
You can not add more items to the array after you have initialized it. What
you could do is this:

C#
--------------------------------------
ArrayList arrStrings = new ArrayList()
arrStrings.Add("1");
arrStrings.Add("2");

//If you need to turn this into an Array after you could just use the
following code

string[] strArray = (string[])arrStrings.ToArray(typeof(string));

//this will le tyou add as many values as you want dynamically and then turn
it back into an array when you are ready
//You could also use the List<T> class instead of the ArrayList class.

I am not sure how this would translate in VB????

Kris




Pim75 said:
Hello,

I'm defining a string array like:
Dim strArray() As String = {"1", "2"}

Can I add some values to this string array later in the code?
It's not clear to me how to do this. I hope someone can help me.

Thanks in advance!
 
G

Guest

David said:
See my earlier post. You can certainly add more items to an array in both C#
and VB (using Array.Resize, and additionally ReDim Preserve in VB), but there
may be performance issues if this is done often. In 2005, List(Of String) is
a good alternative.

Well, neither of you are wrong.

Strictly speaking, you can't add items to an array. It's impossible to
change the size of an array once it's created.

What Array.Resize and ReDim does is to create a new array with the
deisred size and copy all the items from the current array.
 
G

Guest

I mentioned that in my first reply. Array.Resize and ReDim Preserve are
useful tools if you must work with a proper array, provided that one is aware
of the copying that is happening behind the scenes on each resize. If
relatively little resizing is done, then there are minimal performance issues.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
 

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
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top