473,899 Members | 4,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 71624
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.******@tisca li.nlwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.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"
<REMOVEALLCAPIT ALSeEgGoldD...@ mMvVpPsS.orgwro te:
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...@tisca li.nlwrote in message

news:11******** **************@ n59g2000hsh.goo glegroups.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.Collecti ons 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.ToA rray();

// Output ArrayList to our label to see the results.
foreach (string item in myArrayList)
{
lblArrayList.Te xt += 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"
<REMOVEALLCAPIT ALSeEgGoldD...@ mMvVpPsS.orgwro te:
>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...@tisca li.nlwrote in message

news:11******* *************** @n59g2000hsh.go oglegroups.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 StringCollectio n 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.ToA rray(typeof(str ing));

//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.******@tisca li.nlwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.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.ToA rray(typeof(str ing));

//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.******@tisca li.nlwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.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
2877
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 array in a structure, but I think that is to no avail because the size of the string array is variable. Dim strValues() As String = ... Dim myStruct1 As New MyStruct(strValues) Dim wParam As IntPtr = Marshal.AllocHGlobal(myStruct1)...
8
13958
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 the array can vary depending on the parameters passed. What I need to do is loop through the returned array and if applicable write the array element to the screen.
2
1918
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 onlu thing I can do is this: new String* but I cannot specified any initial values... Did someone have any ideas??? Here is an example of what I want to do: C# struct TestingStruct {
2
237
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 could access them as MyStringArray (would give value1) MyStringArray (would give value2) etc......
11
17660
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 s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
5
28412
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 each element but I am having issues using redim to adjust the new array. Any help or example code would be greatly appreciated. thanks!
6
6129
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 more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
2
2229
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 address of the array; 2. When dereferencing it twice (operator **), why the result is always
5
1410
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 retrieve these values in a button click event. here is my code. public String newop(string userna,string pwd) { cwe_pro.GetFolderContentRequestType preq = new cwe_pro.GetFolderContentRequestType(); ...
0
11272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10863
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10971
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10494
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7201
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5887
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4720
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.