473,889 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Yield Keyword in VB.NET 2.0

What the heck - I can't find it. A bit shocked to see it missing though.

So "Does VB.NET have the yield keyword, or any equivalent of it" ?
--

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------
Nov 21 '05
54 6949
Frans,

Therefore, babbling about static/shared is erm... completely
irrelevant ;)

Do you know what the static keyword means in VBNet? (This message is
crossposted sent to the language.vb newsgroup).

It has nothing (slightly because it is Static) to do with shared, as I told
in my first reply. What I only told to keep out confusing from C biased
persons. However ............... .......:-(

I have as well not said it is the same,

Although this text on MSDN gives me the idea that it is a static object.

Using the new yield keyword, your program can return values back to the
foreach statement that called the iterator. The next time the foreach
statement loops and calls the iterator again, the iterator begins its
execution where the previous yield statement left off

Cor
Nov 21 '05 #11
Frans,

Maybe I show it with a sample what my idea was that yield does and why there
is no need for it in VBNet because there is the static keyword.

\\\
Private Sub Whatever
Dim myarray() As String = {"1", "2", "3", "4"}
myproc(myarray, "First")
myproc(myarray, "Second")
End Sub
Private Sub myproc(ByVal myarray() As String, _
ByVal fase As String)
Static i As Integer
For i = i To myarray.Length
Dim y As Integer
If y = 2 Then Exit For
y += 1
Console.Write(i .ToString & " " & fase & vbCrLf)
Next
End Sub
///

Cor
Nov 21 '05 #12
You will always see simple errors in quick made code.

It thought it did work however that is strange, it has to be
\\\
Private Sub Whatever
Dim myarray() As String = {"1", "2", "3", "4"}
myproc(myarray, "First")
myproc(myarray, "Second")
End Sub
Private Sub myproc(ByVal myarray() As String, _
ByVal fase As String)
Static i As Integer
Dim y As Integer
For i = i To myarray.Length
If y = 2 Then Exit For
y += 1
Console.Write(i .ToString & " " & fase & vbCrLf)
Next
End Sub
///
Nov 21 '05 #13
As Frans mentions, Custom iterators are not supported in VB.NET. So it does
not have/need an yield equivalent.

--
Rgds,
Anand
VB.NET MVP
http://www.dotnetindia.com
"Sahil Malik [MVP]" wrote:
What the heck - I can't find it. A bit shocked to see it missing though.

So "Does VB.NET have the yield keyword, or any equivalent of it" ?
--

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
----------------------------------------------------------------------------

Nov 21 '05 #14
"Cor Ligthert" <no************ @planet.nl> schrieb:
Maybe I show it with a sample what my idea was that yield does and why
there is no need for it in VBNet because there is the static keyword.

\\\
Private Sub Whatever
Dim myarray() As String = {"1", "2", "3", "4"}
myproc(myarray, "First")
myproc(myarray, "Second")
End Sub
Private Sub myproc(ByVal myarray() As String, _
ByVal fase As String)
Static i As Integer
For i = i To myarray.Length
Dim y As Integer
If y = 2 Then Exit For
y += 1
Console.Write(i .ToString & " " & fase & vbCrLf)
Next
End Sub
///


This samples doesn't show how to workaround the missing 'yield' keyword in
VB. The sample below is taken from the Beta documentation of Whidbey (C#):

\\\
public class List
{
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while(counter++ < exponent)
{
result = result * number;
yield return result;
}
}

static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach(int i in Power(2, 8))
Console.Write(" {0} ", i);
}
}
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #15
Herfried,

I have used this from MSDN.

In the following example, the foreach loop that calls this iterator will
execute three times, each time receiving the strings in the order specified
by the previous three yield statements:
List list = new List();
foreach(string s in list)
{
Console.WriteLi ne(s);
}
If you want the program to implement the iterator to traverse the elements
in the list, you would modify the iterator to step across the array of
elements using a foreach loop, yielding each item in the array in every
iteration:

public class List
{
internal object[] elements;
internal int count;

public object foreach()
{
foreach(object o in elements)
{
yield o;
}
}
}
However it is not worth the discussion.

It was more that some people where suggesting that static in VBNet is the
same as in C#.

It seems for me that they don't know this VBNet 2003 keyword that has no
equivalent in C# and it seems still in not.

Cor

Nov 21 '05 #16
Cor Ligthert wrote:
Frans,

Therefore, babbling about static/shared is erm... completely
irrelevant ;)
Do you know what the static keyword means in VBNet? (This message is
crossposted sent to the language.vb newsgroup).


Is this a popquiz? No I don't know what 'Static' means in VB.NET, I
don't use VB.NET. But I've looked it up, and it seems similar to
'static' in C if you define a static var in a C-routine. I also fail to
see what the particular relevance of 'Static vars' are in a method, as
they can lead to very unpredictable code, and IF you need to preserve
per-method call values for local vars, I can only think of 1 situation:
recursion. Now, you can better use the stack for that, don't you think?
:)
It has nothing (slightly because it is Static) to do with shared, as
I told in my first reply. What I only told to keep out confusing from
C biased persons. However ............... .......:-(
Great, but what's the relevance to yield ?
I have as well not said it is the same,

Although this text on MSDN gives me the idea that it is a static
object.

Using the new yield keyword, your program can return values back to
the foreach statement that called the iterator. The next time the
foreach statement loops and calls the iterator again, the iterator
begins its execution where the previous yield statement left off


No, it would be a method using object-level static values, and thus
use 'shared' elements, not 'static' elements.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 21 '05 #17
Cor Ligthert wrote:
You will always see simple errors in quick made code.

It thought it did work however that is strange, it has to be
\\\
Private Sub Whatever
Dim myarray() As String = {"1", "2", "3", "4"}
myproc(myarray, "First")
myproc(myarray, "Second")
End Sub
Private Sub myproc(ByVal myarray() As String, _
ByVal fase As String)
Static i As Integer
Dim y As Integer
For i = i To myarray.Length
If y = 2 Then Exit For
y += 1
Console.Write(i .ToString & " " & fase & vbCrLf)
Next
End Sub
///


What are you trying to do in this routine? The second call to myproc i
will be myarray.Length so the loop fails. The static keyword keeps the
value of 'i'. Though take a step back and look at the code you wrote.
No-one will use this kind of code in production software, because there
are far better ways to accomplish the same thing, which ARE more
readable as you can determine what a variable's contents is when the
routine is started (i.e. deterministic code, vs. your non-deterministic
code).

You could mimic an iterator using a wrapper class, and which calls a
delegate on each iteration (to make it generic). Though to set this up
is not transparent, i.e.: it requires work, something which is
transparent for the user of a .NET 2.0 iterator: just iterate over the
structure, whatever the structure represents.

Iterators are just to make foreach loops possible. So you can for
example foreach over a linked list, now that's a bit problematic, (not
undoable). Though there is always a way to do it differently, for
example by using the structures characteristics , in my example of the
linked list, you could use a While Not node.Next Is Nothing (C#:
while(node.Next !=null) {} ). That won't change in .NET 2.0. With
iterators, you just can create another way to iterate over a structure,
one which is usable in foreach. So if you step away from 'I want to use
foreach' and look at the code and just implement what gets the job
done, you will see iterators aren't a requirement, but a convenience
(and a convenience to the user if I'd might add ;))

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 21 '05 #18
Frans,

Have a nice day, maybe it is better that you walk to the end of the 'pier'
take a deep breath and ask yourself: "Is this the tone I have to write in a
newsgroup".

Cor
Nov 21 '05 #19
> What are you trying to do in this routine? The second call to myproc i
will be myarray.Length so the loop fails.
It runs, you did obvious not try it, it gives as result
0 first
1 first
2 second
3 second
Though take a step back and look at the code you wrote.
No-one will use this kind of code in production software, because there
are far better ways to accomplish the same thing,


Did I say that, I don't like the static keyword. It is to much hidden. That
is one of the things that Jon Skeet and I very much agree about.

By the way, based on the view things that I now have read about the 'yield',
have I on first sights the idea about that last as well.

For the rest of your message I don't see the sense therefore I keep it with
this.

Cor
Nov 21 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
2739
by: Sahil Malik [MVP] | last post by:
What the heck - I can't find it. A bit shocked to see it missing though. So "Does VB.NET have the yield keyword, or any equivalent of it" ? -- - Sahil Malik Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync ----------------------------------------------------------------------------
3
6942
by: Ehsan | last post by:
hi coulde any one show me the usage of "yield" keyword specially in this example: """Fibonacci sequences using generators This program is part of "Dive Into Python", a free Python book for experienced programmers. Visit http://diveintopython.org/ for the latest version.
0
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10794
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10896
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9612
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3257
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.