Solved

# get specific item in a vb foreach loop

Posted on 2014-07-21
182 Views
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
``````

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
Question by:maqskywalker
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 26

Expert Comment

ID: 40209805
If number <> 9 Then
Continue For
End If
0

LVL 67

Assisted Solution

sirbounty earned 250 total points
ID: 40209806
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

LVL 9

Accepted Solution

xav056 earned 250 total points
ID: 40209809
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Why Skyport?
###### Suggested Courses
Course of the Month5 days, 9 hours left to enroll