I need a little help here.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carlos Yakimov

    I need a little help here.

    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.ListViewI tem( 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.

  • Tom Dacon

    #2
    Re: I need a little help here.

    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


    "Carlos Yakimov" <masterkid@cant v.netwrote in message
    news:uRLWBmanIH A.3940@TK2MSFTN GP05.phx.gbl...
    >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.ListViewI tem( 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.

    Comment

    • Lloyd Sheen

      #3
      Re: I need a little help here.


      "Tom Dacon" <tdacon@communi ty.nospamwrote in message
      news:uHGgNkbnIH A.5280@TK2MSFTN GP02.phx.gbl...
      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
      >
      >
      "Carlos Yakimov" <masterkid@cant v.netwrote in message
      news:uRLWBmanIH A.3940@TK2MSFTN GP05.phx.gbl...
      >>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.ListViewI tem( 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.
      >
      >
      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

      Comment

      • Lloyd Sheen

        #4
        Re: I need a little help here.


        "Lloyd Sheen" <a@b.cwrote in message
        news:OnmUzPcnIH A.4292@TK2MSFTN GP04.phx.gbl...
        >
        "Tom Dacon" <tdacon@communi ty.nospamwrote in message
        news:uHGgNkbnIH A.5280@TK2MSFTN GP02.phx.gbl...
        >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
        >>
        >>
        >"Carlos Yakimov" <masterkid@cant v.netwrote in message
        >news:uRLWBmanI HA.3940@TK2MSFT NGP05.phx.gbl.. .
        >>>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.ListViewI tem( 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.
        >>
        >>
        >
        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(B yVal sender As System.Object, ByVal e As
        System.EventArg s) 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(ByV al 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

        Comment

        • Tom Dacon

          #5
          Re: I need a little help here.

          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




          Comment

          Working...