Solved

get specific item in a vb foreach loop

Posted on 2014-07-21
3
168 Views
Last Modified: 2014-07-21
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
Comment
Question by:maqskywalker
3 Comments
 
LVL 25

Expert Comment

by:Shaun Kline
Comment Utility
If number <> 9 Then
 Continue For
End If
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 250 total points
Comment Utility
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

by:
xav056 earned 250 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now