Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Add values to string array ?

Reply
Thread Tools

Add values to string array ?

 
 
Pim75
Guest
Posts: n/a
 
      04-13-2007
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!

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      04-13-2007
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" <> wrote in message
news: 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!
>



 
Reply With Quote
 
 
 
 
Pim75
Guest
Posts: n/a
 
      04-13-2007
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.org> wrote:
> 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.nl> wrote in message
>
> news: 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 -



 
Reply With Quote
 
David Longnecker
Guest
Posts: n/a
 
      04-13-2007
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.org> wrote:
>> 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.nl> wrote in message
>>
>> news: 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 -
>>



 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      04-13-2007
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!
>
>

 
Reply With Quote
 
Kris Lankford
Guest
Posts: n/a
 
      04-14-2007
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" <> wrote in message
news: 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!
>

 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      04-14-2007
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<T> class instead of the ArrayList class.
>
> I am not sure how this would translate in VB????
>
> Kris
>
>
>
>
> "Pim75" <> wrote in message
> news: 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!
> >

 
Reply With Quote
 
=?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=
Guest
Posts: n/a
 
      04-14-2007
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
 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      04-14-2007
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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I Add quotes to values in a array? or include quotes.. Richard Sandoval Ruby 5 04-26-2011 09:30 PM
Array of attribute values from object array? danielbigg@googlemail.com Javascript 11 04-13-2009 11:24 AM
Add Array#first= and Array#last= to std lib Robert Klemme Ruby 35 12-31-2007 03:00 PM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
How do you add up all the values in an array? Matt Javascript 7 03-20-2005 10:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57