Results 1 to 2 of 2

Thread: [Resolved][2005] Populate an array

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    47

    Resolved [Resolved][2005] Populate an array

    I am tryin to poplate an array with an unknown number of entries from input boxes. When i try Dim array() i get an warning about unassigned values. Is there anyway to do this? This is what i ahve so far

    Code:
    Dim ipadd() As String
            Dim i As Integer = 0
            For i = 0 To 1
                ipadd(i) = InputBox("Ip address to connect to:")
                If ipadd(i) = "" Then Exit For
            Next
    Last edited by qazwsx; Nov 15th, 2007 at 09:50 PM. Reason: Resolved

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Populate an array

    This:
    vb.net Code:
    1. Dim ipadd() As String
    does NOT create an array. It simply declares a variable that can refer to an array but, at the moment refers to Nothing. To create an array you MUST specify its size.

    Once you've created an array object there's no way to change its size. You can use ReDim or Array.Resize but both of those will create a new array with the new size and then copy the elements from the old.

    What you should be doing is using a collection, which are designed to behave much like arrays but provide the ability to resize dynamically. Internally they actually do use arrays but their use is optimised to reduce the amount of copying that needs to occur.

    Also, you absolutely should NOT be using a For loop in that situation. A For loop is SPECIFICALLY for use when you know the number of iterations you want to perform. In this case it's quite the opposite, so a Do or While loop is more appropriate, e.g.
    vb.net Code:
    1. Dim strings As New List(Of String)
    2. Dim input As String
    3.  
    4. Do
    5.     input = InputBox("IP Address:")
    6.  
    7.     If input <> String.Empty Then
    8.         strings.Add(input)
    9.     End If
    10. Loop Until input = String.Empty
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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