0

I'm having some trouble trying to input integers into an array using visual basic. I'm very new to use visual basic (and programming in general) and I've had a look around google as well as, this website to try and find an answer however I am not finding any luck and I wanted to see if anyone could lend me a hand.

Basically what I have so far

Function inputArray()
    Dim array() As Integer
    Console.WriteLine("Please input how many integers you would like to add")
    For i = 0 To array.Length - 1
        Console.WriteLine("Please enter an integer")
        Console.ReadLine()
    Next
    Console.WriteLine(array)
    Return array
End Function

What i'm trying to achieve is to ask the user how many integers they would like to input into the array and then allow the user to make an input for the amount of integers they have chosen and to store those integers in the array.

If anyone could possibly give me an example piece of code as to how I would do this or any help at all I would really appreciate it.

3
  • In your loop you are not inserting anything to array(). Are you going a windows form here? Commented Mar 22, 2016 at 20:31
  • You need to read the number for how many integers they want to add. Commented Mar 22, 2016 at 20:33
  • I am not using any forms or anything like that just "black box" i think the term is. The problem I have is I do not know how to insert the input into the array and i've been experimenting for a while now trying to figure it out. Also what do you mean by "read the number for how many integers they want to add"? Commented Mar 22, 2016 at 20:38

2 Answers 2

3

You could use a List instead of an Array.

This is a short example (without error-handlings)

Imports system.Threading

Module Module1

    Sub Main()
        Module1.BuildIntegerList()

        Console.ReadKey()
        Environment.Exit(exitCode:=0)
    End Sub

    Private Sub BuildIntegerList()

        Dim values As New List(Of Integer)
        Dim amount As Integer
        Dim nextValue As Integer

        Console.WriteLine("Please input how many integers you would like to add")
        amount = CInt(Console.ReadKey().KeyChar.ToString())

        Do Until values.Count = amount
            Console.Clear()
            Console.WriteLine("Please enter an integer")
            nextValue = CInt(Console.ReadKey().KeyChar.ToString())
            values.Add(nextValue)
            Thread.Sleep(250)
        Loop

        Console.Clear()
        Console.WriteLine(String.Format("Values: {0}", String.Join(", ", values)))

    End Sub

End Module
Sign up to request clarification or add additional context in comments.

1 Comment

I would like to not that you should always check input form user.
1

I would also use List as mentioned by ElektroStudios. However, since you used arrays, here's how I would write it.

 Function inputArray()
     Console.WriteLine("Please input how many integers you would like to add")
     Dim count = CInt(console.ReadLine())
     Dim array(count-1) As Integer 

     For i = 0 To count - 1
            Console.WriteLine("Please enter an integer")
            array(i) = CInt(Console.readline())
     Next

      For i = 0 To array.Length-1
        Console.Write(array(i))
      Next

   return array
 End Function

Here is a working example : dotnetfiddle

2 Comments

Thanks for the responses guys you've been very helpful! Just trying to teach myself how to program so sometimes I need a little help with the logical structuring side to it.
Well.. goodluck! Try to break the problem and deal with it one by one and you'll reach there and yeah Dont forget to upvote and mark an answer that helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.