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. Syntax question

Syntax question

Scheduled Pinned Locked Moved Visual Basic
question
7 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.
  • N Offline
    N Offline
    nlarson11
    wrote on last edited by
    #1

    Hello, I was reading an artical and there was a line of syntax I hadn't seen before and I'm curious what is it doing behind the scenes:

    Dim v as single = 2293.22
    Dim sh as short = New Short?(v)

    Short doesn't have a constructor so how did the '?' allow it? Thanks. Nathan

    'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

    _ J 2 Replies Last reply
    0
    • N nlarson11

      Hello, I was reading an artical and there was a line of syntax I hadn't seen before and I'm curious what is it doing behind the scenes:

      Dim v as single = 2293.22
      Dim sh as short = New Short?(v)

      Short doesn't have a constructor so how did the '?' allow it? Thanks. Nathan

      'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

      _ Offline
      _ Offline
      _Erik_
      wrote on last edited by
      #2

      See nullable types[^].

      N 2 Replies Last reply
      0
      • N nlarson11

        Hello, I was reading an artical and there was a line of syntax I hadn't seen before and I'm curious what is it doing behind the scenes:

        Dim v as single = 2293.22
        Dim sh as short = New Short?(v)

        Short doesn't have a constructor so how did the '?' allow it? Thanks. Nathan

        'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

        J Offline
        J Offline
        Jason Vetter
        wrote on last edited by
        #3

        That's very interesting. I have never seen that either.

        1 Reply Last reply
        0
        • _ _Erik_

          See nullable types[^].

          N Offline
          N Offline
          nlarson11
          wrote on last edited by
          #4

          I guess that was simple. thanks.

          'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

          1 Reply Last reply
          0
          • _ _Erik_

            See nullable types[^].

            N Offline
            N Offline
            nlarson11
            wrote on last edited by
            #5

            Erik, Re-looking it, it obviously has something to do with nullable types but do you have any idea why declaring it as nullable allowed a constructor? Thanks, Nathan.

            'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

            _ 1 Reply Last reply
            0
            • N nlarson11

              Erik, Re-looking it, it obviously has something to do with nullable types but do you have any idea why declaring it as nullable allowed a constructor? Thanks, Nathan.

              'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

              _ Offline
              _ Offline
              _Erik_
              wrote on last edited by
              #6

              Yes, declaring a variable as Nullable(of T) is the same as using T?, where T would be the underlying value type, so for example:

              Dim s as Nullable(Of Short)
              Dim s as Short?

              These two declarations are equivalent. The reason why you can use a constructor is just becouse Nullable(Of T) has one. Replacing Nullable(Of T) with the underlying T? does not change that fact. Here in spain just call it a "syntactical sugar".

              N 1 Reply Last reply
              0
              • _ _Erik_

                Yes, declaring a variable as Nullable(of T) is the same as using T?, where T would be the underlying value type, so for example:

                Dim s as Nullable(Of Short)
                Dim s as Short?

                These two declarations are equivalent. The reason why you can use a constructor is just becouse Nullable(Of T) has one. Replacing Nullable(Of T) with the underlying T? does not change that fact. Here in spain just call it a "syntactical sugar".

                N Offline
                N Offline
                nlarson11
                wrote on last edited by
                #7

                Very helpful. Thank you for the explanation.

                'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous

                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