i have declared an array which i want to populate with names, as they are entered into an input box.It does not seem to work i have tried many other methods with the array but to no success.

Dim namearray() As String
Dim i As Integer

name = InputBox("Enter the candidates name:", "candidatename", "", 500, 500)

name = namearray(i)
i = i + 1

thanks a lot.

Recommended Answers

All 3 Replies

Dim namearray() As String
Dim NewName As String
Dim i As Integer

i = 0
NewName = InputBox("Enter the candidates name:", "candidatename", "", 500, 500)
Do Until String.IsNullOrEmpty(NewName)
  ReDim Preserve namearray(i)
  namearray(i) = NewName
  i += 1
  NewName = InputBox("Enter the candidates name:", "candidatename", "", 500, 500)
Loop

Loops until you enter an empty name.

commented: Well Done Here... Keep Up The Good Work! +10

Why not storing them in a List(Of String)?

Dim listOfNames As New List(Of String)

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

        Dim newName As String = ""
        newName = InputBox("Enter the candidates name:", _
                                  "candidatename", "", 500, 500)
        If Not String.IsNullOrEmpty(newName) Then
            listOfNames.Add(newName)
        End If
        BindListbox()
    End Sub

    Private Sub BindListbox()
        Me.ListBox1.Items.Clear()
        Try
            Me.ListBox1.Items.AddRange(listOfNames.ToArray)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

As per the logic of execution your code, if you want to fill the array in let say button_1 click and then use the array in another sub, you have to declare the array in the form declaration not in the same sub. or else you will be able to use that Array?

' Form1 Declaration
Dim aCandidate(0) As String

' sub Button6_Click
Dim I As Integer
I = UBound(aCandidate, 1) + 1
ReDim Preserve aCandidate(I)
aCandidate(I) = InputBox("Enter the candidates name:", "candidatename", "", 500, 500)
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.