none
add item in String() in VB .net RRS feed

  • Question

  • Hi guys!

    If a have for example Dim myList As String()
    and inside my program I want to add items to this variable
    how can I do this?

    Thanks!

    Tuesday, November 29, 2005 2:45 AM

Answers

  • I could be wrong ( I don't 'do' VB ), but it looks to me like you're creating a string, not an array.  Then again, VB uses () instead of [], so I could be wrong.

    If you want a dynamic array, then you want an ArrayList

    using System.Collections

    Dim myList as ArrayList

    myList.Add("Hello")
    myList.Add("Hi")

    Tuesday, November 29, 2005 6:32 AM
    Moderator
  • Hi,

    cgraus is right, the collection or array list is the way to go.

    if you have code that already uses the array though, this could be the approach you need:

      Dim myStrings() As String
        Dim myStringElements As Integer = 0

        Private Sub btnClickMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClickMe.Click
            Dim i As Integer
            For i = 1 To 5
                AddElementToStringArray("MyString " & i & ".")
            Next

            Me.txtOutput.Text = ("Number of elements in array is: " & myStrings.Length)
        End Sub

        Public Sub AddElementToStringArray(ByVal stringToAdd As String)
            ReDim Preserve myStrings(myStringElements)
            myStrings(myStringElements) = stringToAdd
            myStringElements += 1
        End Sub

    Redim allows the array to grow/shrink, remebre to add Preserve otherwise all the elements currently in the array are lost.

    Paul.
    Tuesday, November 29, 2005 11:01 AM

All replies

  • What do you want to do ? Your 'list' is a string.  You can use += to add to the string, but I doubt that's what you want ?
    Tuesday, November 29, 2005 3:57 AM
    Moderator
  • Hello!
    thanks for the reply!
    What I really want is this
    First I declare Dim myList as String()

    Inside I want to add to the variable some string items like for
    example
    mylist(0) = "Hello"
    myList(1) = "Hi"

    But I want it to be a dynamic string array...

    Tuesday, November 29, 2005 6:29 AM
  • I could be wrong ( I don't 'do' VB ), but it looks to me like you're creating a string, not an array.  Then again, VB uses () instead of [], so I could be wrong.

    If you want a dynamic array, then you want an ArrayList

    using System.Collections

    Dim myList as ArrayList

    myList.Add("Hello")
    myList.Add("Hi")

    Tuesday, November 29, 2005 6:32 AM
    Moderator
  • Hi,

    cgraus is right, the collection or array list is the way to go.

    if you have code that already uses the array though, this could be the approach you need:

      Dim myStrings() As String
        Dim myStringElements As Integer = 0

        Private Sub btnClickMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClickMe.Click
            Dim i As Integer
            For i = 1 To 5
                AddElementToStringArray("MyString " & i & ".")
            Next

            Me.txtOutput.Text = ("Number of elements in array is: " & myStrings.Length)
        End Sub

        Public Sub AddElementToStringArray(ByVal stringToAdd As String)
            ReDim Preserve myStrings(myStringElements)
            myStrings(myStringElements) = stringToAdd
            myStringElements += 1
        End Sub

    Redim allows the array to grow/shrink, remebre to add Preserve otherwise all the elements currently in the array are lost.

    Paul.
    Tuesday, November 29, 2005 11:01 AM
  • Thanks guys!
    Wednesday, November 30, 2005 4:44 AM
  • I know this thread is quite old but I guess you could've used this.

    Dim myList As New List(Of String)
    
    myList.Add("hello")
    
    

    cgraus solution creates a List of objects and it's ok but if what you want is a list of string just say List(Of String)

    obviously you already solved the problem (i hope 6 years is quite a while :-) ), but I'm posting in case someone else is googleing about it.

    regards.

    Diego


    DG
    • Proposed as answer by Dgaleano Monday, August 15, 2011 7:59 PM
    Monday, August 15, 2011 7:59 PM
  • Thank you, Dgaleano!
    Friday, June 22, 2012 5:14 PM
  • Thanks, Dgaleano- for posting (6 years later)! It def helped me.
    Monday, December 3, 2012 4:59 PM
  • Could have been yes, but this thread is from 29th of November 2005, Framework 2.0 was released on the 11th of November 2005 and not everybody was then already known with generic list. Then the arraylist was the best solution.

    :-)


    Success
    Cor

    Monday, December 3, 2012 5:36 PM
  • Hi ALL,

    Seeing as this thread has been resurrected and we should really use a LIST ( I know )

    but here is a way you can add or append an item to the end of an array.

     I made this code generic for one dimensional arrays.

    Add one Button to a Form to try this code please.>>

    Public Class Form1
    
        Private myStrings() As String = {"Hello", " everyone"}
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            MessageBox.Show(String.Concat(myStrings), "Initially...")
            MessageBox.Show("Count of array items = " & myStrings.Length.ToString)
    
            myStrings = AppendArray(myStrings, " how")
            myStrings = AppendArray(myStrings, " are")
            myStrings = AppendArray(myStrings, " you")
            myStrings = AppendArray(myStrings, " today?")
    
            MessageBox.Show(String.Concat(myStrings), "Finally...")
            MessageBox.Show("Count of array items = " & myStrings.Length.ToString)
    
            'Testing code on an array of INTEGER >>
            Dim nums() As Integer = Nothing
            nums = AppendArray(nums, 2)
            MessageBox.Show(nums.Length.ToString)
    
        End Sub
    
        Public Function AppendArray(Of T)(ByVal thisArray() As T, ByVal itemToAppend As T) As T()
    
            If thisArray Is Nothing Then thisArray = New T() {}
            Dim tempList As List(Of T) = thisArray.ToList
            tempList.Add(itemToAppend)
            Return tempList.ToArray
    
        End Function
    
    End Class





    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.


    .

    Tuesday, December 4, 2012 1:45 AM
  • Elegant.  Thanks.
    Friday, September 27, 2013 6:29 AM
  • I know this is an old tread but I was wondering how to do a multidimensional list.

    let's say we want a list that has 2 parameters such as

    myList.myName =

    myList.myData =

    where myName and myData are strings

    Tuesday, March 25, 2014 1:06 PM
  • I know this is an old tread but I was wondering how to do a multidimensional list.

    let's say we want a list that has 2 parameters such as

    myList.myName =

    myList.myData =

    where myName and myData are strings

    You really need to start your own question, but the way that I'd do it would be to have a class with those objects in the class, then you can use a List(Of Class) to store each instance of that class.

    You might also want to override the .ToString method to make short work of displaying the information (per instance).


    Please call me Frank :)

    Tuesday, March 25, 2014 1:24 PM