Add values to string array ?

Discussion in 'ASP .Net' started by Pim75, Apr 13, 2007.

  1. Pim75

    Pim75 Guest

    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!
    Pim75, Apr 13, 2007
    #1
    1. Advertising

  2. 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:...
    > 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!
    >
    Eliyahu Goldin, Apr 13, 2007
    #2
    1. Advertising

  3. Pim75

    Pim75 Guest

    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"
    <> 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" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > 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 -
    Pim75, Apr 13, 2007
    #3
  4. 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"
    > <> 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" <> wrote in message
    >>
    >> news:...
    >>
    >>> 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 -
    >>
    David Longnecker, Apr 13, 2007
    #4
  5. 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!
    >
    >
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Apr 13, 2007
    #5
  6. 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:...
    > 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!
    >
    Kris Lankford, Apr 14, 2007
    #6
  7. 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:...
    > > 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!
    > >
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Apr 14, 2007
    #7
  8. 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
    =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=, Apr 14, 2007
    #8
  9. 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
    >
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Apr 14, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,795
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  2. Robert Klemme
    Replies:
    35
    Views:
    314
    Robert Klemme
    Dec 31, 2007
  3. Richard Sandoval
    Replies:
    5
    Views:
    201
    7stud --
    Apr 26, 2011
  4. Matt
    Replies:
    7
    Views:
    179
  5. Replies:
    11
    Views:
    178
    Zvt.Fred
    Apr 13, 2009
Loading...

Share This Page