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. How do you use default parameters in VB functions?

How do you use default parameters in VB functions?

Scheduled Pinned Locked Moved Visual Basic
c++question
5 Posts 4 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.
  • A Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    I couldn't seem to find the answer from whatever documents I have right now... In C/C++ we have "default parameters" for functions, int Func(int nPara1, int nPara2 = 0);, but how do we achieve this in VB? Thanks a lot.

    I 1 Reply Last reply
    0
    • A Abin

      I couldn't seem to find the answer from whatever documents I have right now... In C/C++ we have "default parameters" for functions, int Func(int nPara1, int nPara2 = 0);, but how do we achieve this in VB? Thanks a lot.

      I Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      Use the Optional keyword eg:

      Function Func(nPara1 as Integer, Optional nPara2 as Integer = 0) as Integer
      

      -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

      A L 2 Replies Last reply
      0
      • I Ian Darling

        Use the Optional keyword eg:

        Function Func(nPara1 as Integer, Optional nPara2 as Integer = 0) as Integer
        

        -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

        A Offline
        A Offline
        Abin
        wrote on last edited by
        #3

        Thanks for your reply, I forgot to mention that I was using VBScript(ASP application) so the "Optional" keyword is not supported :( Is there anyway I can do that in VBScript? Thank you.

        D 1 Reply Last reply
        0
        • A Abin

          Thanks for your reply, I forgot to mention that I was using VBScript(ASP application) so the "Optional" keyword is not supported :( Is there anyway I can do that in VBScript? Thank you.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Option parameters are not supported at all. I haven't tried it, but since all variables in VBScript are of type Variant, you might look into passing an array of values to your function. This could be used to simulate optional arguments, but your function will have to be written to parse up the array and you can't pass parameters by reference at all. RageInTheMachine9532

          1 Reply Last reply
          0
          • I Ian Darling

            Use the Optional keyword eg:

            Function Func(nPara1 as Integer, Optional nPara2 as Integer = 0) as Integer
            

            -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

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

            And C# is missing this functionality (unless you like to have 10's of overloads). This is one of the main reason I am still mainly developing in VB.NET. Has everything C# has (almost) plus some extras... I also feel the IDE is much more responsive with VB.NET... My 2¢. Carl

            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