Results 1 to 9 of 9

Thread: Adding to a String Array

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    37

    Question Adding to a String Array

    Should be simple enough, but I can't get past it. I am trying to add values to a string array, but I keep getting:

    System.NullReferenceException was unhandled
    Message="Object reference not set to an instance of an object." etc ....

    I simplified my code and replicated the problem here:

    VB Code:
    1. Public Class frmTest
    2.  
    3.     Dim x As Integer = 0
    4.     Dim sArrLoad() As String
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         While x < 20
    8.             sArrLoad.SetValue(x, x)
    9.             MsgBox(sArrLoad(sArrLoad.Length))
    10.         End While
    11.  
    12.     End Sub
    13. End Class

    In my project, I am looping through comma delimited lines of text, and I want to load a string array (sArrLoad() ) to use later in the process. How can I do this?

    Any help is greatly appreciated.

    Microsoft Visual Studio.NET 2005 Express (freeeeeee for a limited time)

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: Adding to a String Array

    you need to dimension the array before you try assigning values to it
    i.e.
    VB Code:
    1. 'delcare and dimension the array at the same time
    2. dim sArrLoad(20,20) as string

    or

    VB Code:
    1. 'first declare the array
    2. dim sArrLoad() as string
    3. 'then dimension it later
    4. Redim sArrLoad(20,20)

    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    37

    Re: Adding to a String Array

    Kebo, I know that, but the thing is that the size of the array is going to differ every time I run the program. My only guess was to declare the size at declaration, the ReDim later.

    Microsoft Visual Studio.NET 2005 Express (freeeeeee for a limited time)

  4. #4
    New Member
    Join Date
    Apr 2006
    Posts
    2

    Re: Adding to a String Array

    If you don't always know the size of an array at runtime, use an arraylist instead of a string array.

    You can initialize it with a value and then it will resize dynamically.

    Dim x As Integer = 0
    Dim arrOfStrings As ArrayList

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    arrOfStrings = New ArrayList(10)

    While x < 20
    arrOfStrings.Add(x.ToString)
    MessageBox.Show(arrOfStrings.Item(x))
    x = x + 1
    End While

    End Sub

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Adding to a String Array

    You don't need to initialize the size of an arraylist. It is done automatically when you all add. If you would like to resize an array of some object you can do this:
    VB Code:
    1. Dim str(10) As String
    2.  
    3. For i As Integer = 0 to 15
    4.   If i > str.Length -1 Then
    5.     ReDim Preserve str(str.Length) ' enlarges the string by 1
    6.     'if you don't use Preserve everything in the array will be lost when resizing it
    7.   End If
    8.    str(i) = "Test " & CStr(i)
    9. Next

    I beleive it is recomened that you should resize it by a large amount instead of doing it one at a time like this loop will do.

    To use an array list
    VB Code:
    1. Dim str As New ArrayList 'no need to initialize a size
    2.  
    3. For i As Integer = 0 to 15
    4.     str.Add("Test " & CStr(i))
    5. Next
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    37

    Re: Adding to a String Array

    Thanks for all the suggestions guys, I will check them out and rate all of you.

    Microsoft Visual Studio.NET 2005 Express (freeeeeee for a limited time)

  7. #7
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Adding to a String Array

    If your going to use an array instead of an arraylist its recommended to double the size of the array each time you reach its limit. Then you can redim the array to the actual size after knowning how many spaces are needed in the array.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  8. #8
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: Adding to a String Array

    If you are working with strings, you should actually use a string collection instead of an arraylist, and works pretty much the same way.

    VB Code:
    1. Dim MyStrings As New System.Collections.Specialized.StringCollection
    2.         MyStrings.Add("This")
    3.         MyStrings.Add("That")
    4.         'just displays to see that it added them...
    5.         For Each Str As String In MyStrings
    6.             MessageBox.Show(Str)
    7.         Next

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2005
    Posts
    37

    Re: Adding to a String Array

    Sweet, I have about 5 options to go with, you guys are great! I will close this issue and thanks again.

    Microsoft Visual Studio.NET 2005 Express (freeeeeee for a limited time)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width