Solved

get specific item in a vb foreach loop

Posted on 2014-07-21
3
179 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 26

Expert Comment

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

Assisted Solution

by:sirbounty
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

by:
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Web Form VB.Net  import CSV 4 45
vb.net help 4 34
How do sunrise and sunset times change with altitude 14 38
MYSQL responding very slow 3 26
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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