Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Converting VB's IIf to Conditional Operator in c#

Converting VB's IIf to Conditional Operator in c#

Scheduled Pinned Locked Moved Visual Basic
csharphelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael9000
    wrote on last edited by
    #1

    It's been a while since I've used VB and I need help with understanding the truePart in the second line.

    Dim asciiBytes As Byte() = Encoding.ASCII.GetBytes("SomeAsciiData")

    Dim nextchar As Integer = IIf(asciiBytes.Length > (i + 1), asciiBytes.Length > (i + 1), -1)

    How does asciiBytes.Length > (i + 1) return an integer?

    G 1 Reply Last reply
    0
    • M Michael9000

      It's been a while since I've used VB and I need help with understanding the truePart in the second line.

      Dim asciiBytes As Byte() = Encoding.ASCII.GetBytes("SomeAsciiData")

      Dim nextchar As Integer = IIf(asciiBytes.Length > (i + 1), asciiBytes.Length > (i + 1), -1)

      How does asciiBytes.Length > (i + 1) return an integer?

      G Offline
      G Offline
      Geoff Williams
      wrote on last edited by
      #2

      In VB.NET an expression like

      asciiBytes.Length > (i + 1)

      will return a Boolean value - in this case True as it is identical to the Expression part of the IIf method. Internally, a Boolean value is stored as an integer (False -> 0, True -> -1), so the conversion to an integer is straightforward. However, I think there' a bug in your code: it should probably read

      Dim nextchar As Integer = IIf(asciiBytes.Length > (i + 1), asciiBytes(i + 1), -1)

      in other words it returns the ASCII value (0 - 255) of the next character in the array if the Expression is True and -1 if it is False.

      M 1 Reply Last reply
      0
      • G Geoff Williams

        In VB.NET an expression like

        asciiBytes.Length > (i + 1)

        will return a Boolean value - in this case True as it is identical to the Expression part of the IIf method. Internally, a Boolean value is stored as an integer (False -> 0, True -> -1), so the conversion to an integer is straightforward. However, I think there' a bug in your code: it should probably read

        Dim nextchar As Integer = IIf(asciiBytes.Length > (i + 1), asciiBytes(i + 1), -1)

        in other words it returns the ASCII value (0 - 255) of the next character in the array if the Expression is True and -1 if it is False.

        M Offline
        M Offline
        Michael9000
        wrote on last edited by
        #3

        Thanks Geoff I didn't write the code (but that line is working) - maybe the compiler converts the boolean to a 0 or 1 in the background. I'll test it later

        L 1 Reply Last reply
        0
        • M Michael9000

          Thanks Geoff I didn't write the code (but that line is working) - maybe the compiler converts the boolean to a 0 or 1 in the background. I'll test it later

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Correctly, true equals -1, and 0 equals to false. Perhaps it would be easier to just initialize the variable to -1?

          I are Troll :suss:

          M 1 Reply Last reply
          0
          • L Lost User

            Correctly, true equals -1, and 0 equals to false. Perhaps it would be easier to just initialize the variable to -1?

            I are Troll :suss:

            M Offline
            M Offline
            Michael9000
            wrote on last edited by
            #5

            Absolutely right. :thumbsup: (I incorrectly assumed that true would be 1 and not -1)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups