Yield Keyword in VB.NET 2.0

  • Thread starter Thread starter Sahil Malik [MVP]
  • Start date Start date
S

Sahil Malik [MVP]

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" ?
 
Cor,

I checked that place - no help there.

I must say, I am incredibly frustrated and very annoyed that something as
important as the "yield" keyword would be missed out of VB.NET.

Now try writing a SQLCLR TVF and the 3 lines it takes to accomplish it in C#
translate to 3 pages of buggy VB.NET code. Highly Highly Highly Highly
stupid to miss that out IMO.

- Sahil Malik [MVP]
Upcoming ADO.NET 2.0 book - http://tinyurl.com/9bync
 
So "Does VB.NET have the yield keyword, or any equivalent of it" ?

No. Different languages have different features.



Mattias
 
Sahil,
I'm not talking about "Static/Shared" .. I am talking about "Yield".
I assume that you do not know what Static is in VBNet, therefore I gave you
the link to the page.

If you want a mercedes with a chevrolet logo, than you have to put that logo
on it yourself, Mercedes does not do that for you.

However I find the Mercedes logo is not bad at all

static d as integer
For each a in b
d += 1
Next

This gives you at the end of your program the total result of loops or if
you set it conditional to zero let you go on where you was in the loop.

I am not busy with 2005, however short reading gave me the idea that this
was the idea.

I hope this helps,

Cor
 
Thanks Mattias. Finally a direct answer to a simple question.

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

I assume that you do not know what Static is in VBNet, therefore I
gave you the link to the page.

If you want a mercedes with a chevrolet logo, than you have to put
that logo on it yourself, Mercedes does not do that for you.

However I find the Mercedes logo is not bad at all

static d as integer
For each a in b
d += 1
Next

This gives you at the end of your program the total result of loops
or if you set it conditional to zero let you go on where you was in
the loop.

I am not busy with 2005, however short reading gave me the idea that
this was the idea.

yield is a keyword which is only valid in custom enumerator routines
('iterators'), something which is C# 2.0 only if I'm not mistaken. It's
therefore logical VB.NET doesn't have a yield equivalent, as it doesn't
support custom iterator creation (though it's my understanding it does
support consuming them). At least, that's my understanding, but I might
have overlooked something.

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

FB

--
 
Sahil said:
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" ?

yield is a keyword for custom iterators, which IMHO is something
which is C# only.

However, it's not hard to work around it. After all, we all write code
today which works OK right? :)

FB

--
 
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
 
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
 
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
///
 
As Frans mentions, Custom iterators are not supported in VB.NET. So it does
not have/need an yield equivalent.
 
Cor Ligthert said:
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);
}
}
///
 
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.WriteLine(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
 
Cor said:
Frans,

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

--
 
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
 
Cor said:
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

--
 
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
 
Back
Top