I would like to know how to take input from a user and populate an array?

I know how to do an array normally. I know how to have a user input information into a file that I create. But, I don't know how to put the two together.

Any help would be appreciated!

Dani AI

Generated

Good answers already in this thread: on pre-dimensioning when the count is known, demonstrating InputBox-based entry, and showing ReDim Preserve for incremental collection. The notes below clarify tradeoffs and add concise, modern patterns (VB.NET and VB6) plus practical pitfalls to avoid.

VB.NET pattern (preferred when available): use a List(Of T) to gather inputs, then convert to an array only if required. This avoids repeated resizing and is simple to persist/read with the System.IO helpers.

' collect inputs
Dim names As New List(Of String)
' on each input event:
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
    names.Add(TextBox1.Text.Trim())
    TextBox1.Clear()
End If

' persist and convert
Dim path = IO.Path.Combine(Application.StartupPath, "names.txt")
IO.File.WriteAllLines(path, names)
Dim arr() As String = names.ToArray()

VB6 pattern: a Collection gives dynamic growth without the cost of many ReDim Preserve operations; convert to a zero-based array only when needed.

Dim c As New Collection
' during input loop: c.Add inputValue

Dim arr() As String
ReDim arr(c.Count - 1)
Dim i As Integer
For i = 1 To c.Count
    arr(i - 1) = c(i)
Next

Troubleshooting and quick rules:

  • For known sizes, pre-dimension (as suggested) to avoid resizes.
  • ReDim Preserve is handy but expensive when done repeatedly; prefer List(Of T) or Collection for many items.
  • Validate input (Trim, check Empty/Cancel, use Integer.TryParse or IsNumeric) to avoid silent errors.
  • Beware VB6 Option Base and UBound/LBound behavior; VB.NET arrays are zero-based.
  • Use File.WriteAllLines / File.ReadAllLines in .NET or proper Open/Close with error handling in VB6 when persisting.

These patterns complement the InputBox and ReDim examples already posted while addressing performance, validation, and file-IO in a maintainable way.

Recommended Answers

All 3 Replies

First Dimension an array with the required number of elements. Design a form with textbox or something to accept input.
Form a loop to run up to the size of the array. Inside the loop get the imput from the user, and the assign stright to the array element with the index.

If you are using a file. Try to write it from the input each line output.
(Otherwise you have to seperate two data with some delimiters.)

open this file in input mode
Run a loop either to EOF or to the size of your dimensioned array.
inside the loop
read the first line from the file
just assign to each elements of the array.
End loop
OVER

'hope this code snippet can serve your purpose

Dim n(4), s As String, i As Integer

'populating the string array
For i = 0 To 4 Step 1
begin:
s = InputBox("Demo of populating an array by accepting name of 5 students." & Chr(10) & "Enter name of student" & i + 1 & " :", "Populating Array")
If s <> "" Then
n(i) = s
Else
MsgBox "No student has null in name."
GoTo begin
End If
Next i

'creating and writing array values into file
Open App.Path & "\name.txt" For Output As #1
For i = 0 To 4 Step 1
Print #1, "Information About Student"
Print #1, "---------------------------------"
Print #1, "Name : " & n(i)
Print #1, "Position in array : " & i
Print #1, "Holding rank : " & i + 1
Print #1, "---------------------------------"
Print #1, ""
Next i
Close #1

Dim arr() As String

Private Sub Command1_Click()Static i As Integer

ReDim Preserve arr(i)

arr(i) = Me.Text1

i = i + 1
End Sub

Private Sub Command2_Click()Dim i As Integer

Me.Cls

For i = 0 To UBound(arr)Me.Print arr(i)
Next i

End Sub

'Hope it will help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.