1

Like title, running this code shows an array with all 0 values in it.

I have tried using integer array too but I can't

Dim woohoo(9) As String
Dim count As Integer = 2
For Each number As String In woohoo
    number = CStr(count)
    count += 2
Next

For Each number As Integer In woohoo
    Console.WriteLine(number)
Next

2,4,6,8,10...20

3
  • Why have you got two loops over the same array with one treating the elements as Strings and one treating them as Integers? Make up your mind whether you want to work with Strings or Integers and do it. Commented Oct 13, 2019 at 11:09
  • As for the issue, where do you actually put anything into the array? Nowhere. I see you setting the number variable but that's not the array. If you want something in the array, put something in the array. You're not going to do that with a For Each loop, which is only for using items already in a list. Commented Oct 13, 2019 at 11:10
  • Thanks, I just knew I never used For Each loop for inserting data before. Now I understand, I'll refer to other For Loops. Commented Oct 14, 2019 at 12:39

2 Answers 2

1

You need to do this to SET the values of an array:

Dim wooho(9) As Integer
Dim count As Integer = 2

For i As Integer = 0 To wooho.Count - 1
   wooho(i) = count
   count += 2
   Console.WriteLine(wooho(i))
Next

And this to GET the values:

For Each number As Integer In wooho
    Console.WriteLine(number)
Next
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest using a list of Integers for this. Try this:

    Dim woohoo As New List (Of Integer)
    Dim count As Integer = 2 
    For i As Integer = 0 To 9
        woohoo.Items.Add (count)
        count += 2
    Next

    For Each number As Integer In woohoo
        Console.WriteLine(CStr(number))
    Next

3 Comments

If the size is not changing, there's no point to using a List rather than an array.
@jmcilhinney I used a list instead of an array because of easier iterations and less code.
Neither of those is a thing. You could just change woohoo.Items.Add (count) to woohoo(i) = count. How is that harder or more code?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.