|
-
Nov 15th, 2007, 08:59 PM
#1
Thread Starter
Member
[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
-
Nov 15th, 2007, 09:13 PM
#2
Re: [2005] Populate an array
This: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:
Dim strings As New List(Of String)
Dim input As String
Do
input = InputBox("IP Address:")
If input <> String.Empty Then
strings.Add(input)
End If
Loop Until input = String.Empty
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|