Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

get specific item in a vb foreach loop

i saw this example at this link on a foreach loop

http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

My question is on this foreach loop

Dim numberSeq() As Integer =
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

For Each number As Integer In numberSeq
    ' If number is between 5 and 7, continue 
    ' with the next iteration. 
    If number >= 5 And number <= 8 Then 
        Continue For 
    End If 

    ' Display the number.
    Debug.Write(number.ToString & " ")

    ' If number is 10, exit the loop. 
    If number = 10 Then 
        Exit For 
    End If 
Next
Debug.WriteLine("")
' Output: 1 2 3 4 9 10

Open in new window


How would I revise the if statement inside the for each loop so I only get this output.

Output: 9

So essentially, I want to iterate through the list of numbers put only show the number 9
0
maqskywalker
Asked:
maqskywalker
2 Solutions
 
Shaun KlineLead Software EngineerCommented:
If number <> 9 Then
 Continue For
End If
0
 
sirbountyCommented:
Dim numberSeq() As Integer =
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

For Each number As Integer In numberSeq
    ' If number is between 5 and 7, continue
    ' with the next iteration.
    If number = 9 Then
      debug.write "Output: " & number
      exit for
    End If
Next
Debug.WriteLine("")
' Output: 1 2 3 4 9 10
0
 
xav056Commented:
Dim numberSeq() As Integer =
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

For Each number As Integer In numberSeq
    ' If number is 9 display it when 10 is hit exit the for loop
    If number =9 Then
        Debug.Write(number.ToString & " ")
    End If

   
    ' If number is 10, exit the loop.
    If number = 10 Then
        Exit For
    End If
Next
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now