Results 1 to 7 of 7

Thread: [RESOLVED] C# Expression to VB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2016
    Posts
    106

    Resolved [RESOLVED] C# Expression to VB

    I ran across some C# code that I am duplicating in my VB project. Its pretty cool because it stores the incremented readbyte as integer in b while increment the readbyte and checking to see if it is not -1 at the same time.



    Code:
     
    int b;
    while ((b = inputStream.ReadByte()) != -1){
    
    }
    How do I write this in VB?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: C# Expression to VB

    Since C# has a ancestry based on C, any assignment can be treated as an expression which returns a value, so you can chain assignments together.
    You can't do that in VB, so you would have to rewrite the logic a bit.
    Usually, I'll call the method once outside the loop, and then again at the bottom of the loop.
    Code:
    Dim b as Integer
    
    b = inputStream.ReadByte()
    Do While b <> -1
      'other code
    
      b = inputStream.ReadByte()
    Loop
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2016
    Posts
    106

    Re: C# Expression to VB

    Quote Originally Posted by passel View Post
    Since C# has a ancestry based on C, any assignment can be treated as an expression which returns a value, so you can chain assignments together.
    You can't do that in VB, so you would have to rewrite the logic a bit.
    Usually, I'll call the method once outside the loop, and then again at the bottom of the loop.
    Code:
    Dim b as Integer
    
    b = inputStream.ReadByte()
    Do While b <> -1
      'other code
    
      b = inputStream.ReadByte()
    Loop
    Yup that did it, thank you very much!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: C# Expression to VB

    If your issue is resolved, please use the Thread Tools menu to mark the thread Resolved. That way, we can see that you no longer need help without opening the thread and reading the whole thing.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2016
    Posts
    106

    Re: C# Expression to VB

    Quote Originally Posted by jmcilhinney View Post
    If your issue is resolved, please use the Thread Tools menu to mark the thread Resolved. That way, we can see that you no longer need help without opening the thread and reading the whole thing.
    Oh yup my bad!

  6. #6
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: [RESOLVED] C# Expression to VB

    If you want something that is maybe more close to the c# eg (coding wise at least) you could go:

    VB.Net Code:
    1. Dim b As Integer
    2. While (Function()
    3.            b = inputstream.ReadByte()
    4.            Return b
    5.        End Function).Invoke() <> -1
    6.  
    7. End While

    Kris

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] C# Expression to VB

    Thank's i00.
    I tried to come up with that originally, but couldn't figure out the proper form with a function, so gave up fairly quickly.
    I still wouldn't do it that way in any case, as I find it arcane (i.e. I couldn't easily figure out how to do it, so....), and I'm assuming it is probably slower overall compared to not using a delegate, but I would have liked to have been able to figured it out so could have posted both. One as a possible closer to literal implementation of the code (i.e. the delegate function call) and the second as the better choice functionally, IMHO.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width