I need a little help here.

C

Carlos Yakimov

I want to convert this C# Linq Query into VB.NET

void Load() {
int i = 1;
var q = from P in Persons
select new System.Windows.Forms.ListViewItem( P.Name, i++ );
}

I did this:

Sub Load()
Dim I As Integer = 1
Dim q = from P in Persons _
Select New ListViewItem(P.Name, i++
? )
End Sub

My problem is how I can use the ++ Operator in Vb?

Thanks in advance.
 
T

Tom Dacon

Carlos, the ++ operator in C# uses the current value, if it's involved in an
expression of any kind, and then increments it after use. So in the C# code,
it will use the i value of 1 as an argument in the select, and then will set
it to 2 afterwards.

So the equivalent in VB.Net would be:

Sub Load()

Dim i As Integer = 1

Dim q = from P in Persons _
Select New ListViewItem(P.Name, i)
i += 1

' Other code follows, presumably...

End Sub


Tom Dacon
Dacon Software Consulting
 
L

Lloyd Sheen

Tom Dacon said:
Carlos, the ++ operator in C# uses the current value, if it's involved in
an expression of any kind, and then increments it after use. So in the C#
code, it will use the i value of 1 as an argument in the select, and then
will set it to 2 afterwards.

So the equivalent in VB.Net would be:

Sub Load()

Dim i As Integer = 1

Dim q = from P in Persons _
Select New ListViewItem(P.Name, i)
i += 1

' Other code follows, presumably...

End Sub


Tom Dacon
Dacon Software Consulting

I tried this and the C# version would provide an incremented value for each
listviewitem created. So you would expect a collection of listviewitems
where the imageIndex would start at 1 and increment for each item.

When you try the code in VB (and I have no idea why) you end up with a list
of listviewitems and each has an imageIndex of 2. If I change the code to i
+=2 then the items have an imageIndex of 3. It seems that the final
increment is what is used as if the value is not created until used.

LS
 
L

Lloyd Sheen

Lloyd Sheen said:
I tried this and the C# version would provide an incremented value for
each listviewitem created. So you would expect a collection of
listviewitems where the imageIndex would start at 1 and increment for each
item.

When you try the code in VB (and I have no idea why) you end up with a
list of listviewitems and each has an imageIndex of 2. If I change the
code to i +=2 then the items have an imageIndex of 3. It seems that the
final increment is what is used as if the value is not created until used.

LS

Ok I think you can use a function to do the incrementing:

Dim Persons() As String = {"Him", "Her", "Them"}


Dim ii As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tst = From t In Persons _
Select New ListViewItem(t, GiveMeAnInt(t))

Dim i As Integer = tst.Count
Dim tl As List(Of ListViewItem) = tst.ToList
End Sub

Private Function GiveMeAnInt(ByVal tmp As String) As Integer
ii += 1
Return ii
End Function


Once again though the imageIndex values are in fact 4,5,6 which I think
confirms that somehow the values are being put in at a time other than what
I would think is correct. Linq has its values but I don't think that in
the case of VB that this works very well.


Tracing the call to GiveMeAnInt shows 6 calls. Seems like the .ToList
recalls the values so if you want to create the list you would have to put
an ii=0 prior to the .ToList call. My last iteration also removed the need
to have a parameter to the GiveMeAnInt. All in all quite a kludge.

Lloyd Sheen
 
T

Tom Dacon

You know, I just realized that I was completely off base with my suggestion.
I haven't actually used Linq (we're still on 2.0), and I was just thinking
that what I was seeing in the OP's code was a simple function call. Clearly
the constructor's being called once for each hit in the select. So my
suggestion wasn't worth a damn.

Sorry 'bout that.

Tom Dacon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top