Important alert: (current site time 7/15/2013 4:27:55 AM EDT)
 

VB icon

Using Yield Keyword

Email
Submitted on: 6/24/2013 10:28:31 PM
By: Salem Al Shekaili 
Level: Beginner
User Rating: Unrated
Compatibility: VB.NET
Views: 337
 
     Four examples of using yield Keyword in VB .Net 2012
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Using Yield Keyword
// Description:Four examples of using yield Keyword in VB .Net 2012
// By: Salem Al Shekaili
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=9098&lngWId=10//for details.//**************************************

Module Module1
Sub Main()
 
'example 1
ShowingFiles()
'example 2
Oddnumber()
'example 3
prime()
'example 4
Some_number()
Console.ReadKey()
End Sub
Private Sub prime()
For Each numbers As Long In PrimesNumber(50)
Console.WriteLine(numbers.ToString() + Environment.NewLine)
Next
End Sub
'example 4
Private Sub Some_number()
For Each number As Integer In SomeNumbers()
Console.Write(number & " ")
Next
End Sub
Private Sub ShowingFiles()
For Each file_Name In FileToProcessIterator()
' Do something; process file
Console.WriteLine(file_Name)
Next
End Sub
Private Iterator Function FileToProcessIterator() As IEnumerable(Of String)
For Each file_Name In System.IO.Directory.EnumerateFiles("c:\", "*.*")
Yield file_Name
Next
End Function
Public Iterator Function ReturnTenThings10() As IEnumerable(Of String)
Dim Ret As New List(Of String)()
For I As Integer = 0 To 9
Threading.Thread.Sleep(1000)
Yield (I + 1).ToString()
Next
End Function
Private Iterator Function SomeNumbers() As System.Collections.IEnumerable
Yield 3
Yield 5
Yield 8
End Function
Private Iterator Function CityNames() As System.Collections.IEnumerable
Dim str As String() = {"Abu Dhabi", "Londan", "Dubai", "Sharjah", "Ras Al kymah"}
For i As Integer = 0 To str.Length - 2
Yield str(i) & " City "
Next
End Function
Private Sub Oddnumber()
For Each strCity As String In CityNames()
System.Console.Write(strCity & " ")
Next
End Sub
'This below function shows how to find a number's prime factors in Visual Basic .NET.
Private Iterator Function PrimesNumber(num As Integer) As IEnumerable(Of Long)
Dim isPrime As Boolean = False
' Take out the 2s.
For i As Integer = 2 To num
isPrime = False
If i = 2 Then
Yield i
Continue For
End If
' We don't care about even numbers (except 2)
If i Mod 2 = 0 Then
Continue For
End If
isPrime = True
For j As Integer = 2 To CLng(Math.Sqrt(i))
' Check if current value is divisible by something
' other than 1 and itself
If i Mod j = 0 Then
isPrime = False
Exit For
End If
Next
If isPrime Then Yield i
Next
End Function
End Module


Other 8 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.