473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Add values to string array ?

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!

Apr 13 '07 #1
8 71551
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
"Pim75" <p.******@tiscali.nlwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
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!

Apr 13 '07 #2
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.
On 13 apr, 14:32, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote:
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

"Pim75" <p.meg...@tiscali.nlwrote in message

news:11**********************@n59g2000hsh.googlegr oups.com...
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 -

Apr 13 '07 #3
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.

On 13 apr, 14:32, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote:
>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
"Pim75" <p.meg...@tiscali.nlwrote in message

news:11**********************@n59g2000hsh.googleg roups.com...
>>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 -

Apr 13 '07 #4
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
"Pim75" wrote:
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!

Apr 13 '07 #5
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<Tclass instead of the ArrayList class.

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

Kris


"Pim75" <p.******@tiscali.nlwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
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!
Apr 14 '07 #6
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" wrote:
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<Tclass instead of the ArrayList class.

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

Kris


"Pim75" <p.******@tiscali.nlwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
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!
Apr 14 '07 #7
David Anton wrote:
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öran Andersson
_____
http://www.guffa.com
Apr 14 '07 #8
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
"Göran Andersson" wrote:
David Anton wrote:
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öran Andersson
_____
http://www.guffa.com
Apr 14 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rob Mayo | last post by:
How can I create a pointer to a string array from VB.Net? I want to take a string array of variable size and pass it as a wParam on a message to a custom subclassed window. I've tried wrapping the...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
2
by: rmathieu | last post by:
Hi, I want to initialize a static String array in MC++. What I want to do is to initialize my String array like the C# way: new String {"11", "22"} but I could not find an equivalent in MC++. The...
2
by: Andrew Banks | last post by:
I've currently got a string formatted as values; string MyString = "value1;value2;value3;value4;" How would I split this and place the values into a string array called MyStringArray so I...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
2
by: LinLMa | last post by:
Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the...
5
by: sarabonn | last post by:
hallo, I have a function which takes 2 values as input and i want to return that 2 values, so how should write the return statement to return 2 values of String array. I also i should...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.