Increment operators ?
Posted
Tue, Mar 25 2008 18:00
by
bill
Does VB need prefix and postfix increment and decrement operators ? Here's an example I posted today for a question on adding an index with LINQ:
Sub Main()
Dim values() As String = {"aaa", "bbb", "ccc"}
Dim index As Int32 = -1
Dim view = From v In values _
Let x = increment(index) _
Select x, v
For Each item In view
Console.WriteLine(item.x & " : " & item.v)
Next
Console.ReadLine()
End Sub
Public Function increment(ByRef value As Int32) As Int32
value += 1
Return value
End Function
And this is the same code in C#:
static void Main(string[] args)
{
string[] values = { "aaa", "bbb", "ccc" };
int index = -1;
var view = from v in values
let x = ++index
select new{x, v};
foreach (var item in view)
{
Console.WriteLine(item.x + " : " + item.v);
}
Console.ReadLine();
}
Note in C# I didn't need to write the increment function.