Default values for args

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

    #1

    Default values for args

    Hi,

    Is there a way to set defaults values for args of a function/sub?

    For example:

    Function:
    public function testFunc (name as string, age as integer = 30, phone as
    string)

    Calling the Function:
    testFunc("John" , 25, "555-1212")
    testFunc("John" , , "555-1212") ' Age will be set to default of 30

    This way one or more of arg can be left out and still have a working
    function/sub.

    Thank you.


  • Brx

    #2
    Re: Default values for args

    Your so close, it's Public Function testFunc(name as string, phone as
    string, optional age as integer = 30)
    Optional parameters must be last in the function, so anything after age must
    also be optional.

    kmurphy
    roughly at nexusinfosys in the com domain.

    "Dragon" <baadil_nospam@ hotmail.com> wrote in message
    news:O$oWwG00FH A.2132@TK2MSFTN GP15.phx.gbl...[color=blue]
    > Hi,
    >
    > Is there a way to set defaults values for args of a function/sub?
    >
    > For example:
    >
    > Function:
    > public function testFunc (name as string, age as integer = 30, phone as
    > string)
    >
    > Calling the Function:
    > testFunc("John" , 25, "555-1212")
    > testFunc("John" , , "555-1212") ' Age will be set to default of 30
    >
    > This way one or more of arg can be left out and still have a working
    > function/sub.
    >
    > Thank you.
    >
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Default values for args

      "Dragon" <baadil_nospam@ hotmail.com> schrieb:[color=blue]
      > Is there a way to set defaults values for args of a function/sub?
      >
      > For example:
      >
      > Function:
      > public function testFunc (name as string, age as integer = 30, phone as
      > string)
      >
      > Calling the Function:
      > testFunc("John" , 25, "555-1212")
      > testFunc("John" , , "555-1212") ' Age will be set to default of 30
      >
      > This way one or more of arg can be left out and still have a working
      > function/sub.[/color]

      You can archieve this behavior with optional parameters:

      \\\
      Public Sub TestFunc( _
      ByVal Name As String, _
      Optional ByVal Age As Integer = 30, _
      Optional ByVal ID As String = Nothing _
      )
      ...
      End Sub
      ///

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Bob Powell [MVP]

        #4
        Re: Default values for args

        <sigh> I really miss this in C#...

        --
        Bob Powell [MVP]
        Visual C#, System.Drawing

        Ramuseco Limited .NET consulting


        Find great Windows Forms articles in Windows Forms Tips and Tricks


        Answer those GDI+ questions with the GDI+ FAQ


        All new articles provide code in C# and VB.NET.
        Subscribe to the RSS feeds provided and never miss a new article.





        "Brx" <To.News.Group@ plz> wrote in message
        news:e6bE7N00FH A.3924@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Your so close, it's Public Function testFunc(name as string, phone as
        > string, optional age as integer = 30)
        > Optional parameters must be last in the function, so anything after age
        > must also be optional.
        >
        > kmurphy
        > roughly at nexusinfosys in the com domain.
        >
        > "Dragon" <baadil_nospam@ hotmail.com> wrote in message
        > news:O$oWwG00FH A.2132@TK2MSFTN GP15.phx.gbl...[color=green]
        >> Hi,
        >>
        >> Is there a way to set defaults values for args of a function/sub?
        >>
        >> For example:
        >>
        >> Function:
        >> public function testFunc (name as string, age as integer = 30, phone as
        >> string)
        >>
        >> Calling the Function:
        >> testFunc("John" , 25, "555-1212")
        >> testFunc("John" , , "555-1212") ' Age will be set to default of 30
        >>
        >> This way one or more of arg can be left out and still have a working
        >> function/sub.
        >>
        >> Thank you.
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Dragon

          #5
          Re: Default values for args

          Thank you :-)

          "Brx" <To.News.Group@ plz> wrote in message
          news:e6bE7N00FH A.3924@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Your so close, it's Public Function testFunc(name as string, phone as
          > string, optional age as integer = 30)
          > Optional parameters must be last in the function, so anything after age
          > must also be optional.
          >
          > kmurphy
          > roughly at nexusinfosys in the com domain.
          >
          > "Dragon" <baadil_nospam@ hotmail.com> wrote in message
          > news:O$oWwG00FH A.2132@TK2MSFTN GP15.phx.gbl...[color=green]
          >> Hi,
          >>
          >> Is there a way to set defaults values for args of a function/sub?
          >>
          >> For example:
          >>
          >> Function:
          >> public function testFunc (name as string, age as integer = 30, phone as
          >> string)
          >>
          >> Calling the Function:
          >> testFunc("John" , 25, "555-1212")
          >> testFunc("John" , , "555-1212") ' Age will be set to default of 30
          >>
          >> This way one or more of arg can be left out and still have a working
          >> function/sub.
          >>
          >> Thank you.
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Dragon

            #6
            Re: Default values for args

            Is it not available in C#?


            "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
            news:%23zxNKV00 FHA.1168@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > <sigh> I really miss this in C#...
            >
            > --
            > Bob Powell [MVP]
            > Visual C#, System.Drawing
            >
            > Ramuseco Limited .NET consulting
            > http://www.ramuseco.com
            >
            > Find great Windows Forms articles in Windows Forms Tips and Tricks
            > http://www.bobpowell.net/tipstricks.htm
            >
            > Answer those GDI+ questions with the GDI+ FAQ
            > http://www.bobpowell.net/faqmain.htm
            >
            > All new articles provide code in C# and VB.NET.
            > Subscribe to the RSS feeds provided and never miss a new article.
            >
            >
            >
            >
            >
            > "Brx" <To.News.Group@ plz> wrote in message
            > news:e6bE7N00FH A.3924@TK2MSFTN GP14.phx.gbl...[color=green]
            >> Your so close, it's Public Function testFunc(name as string, phone as
            >> string, optional age as integer = 30)
            >> Optional parameters must be last in the function, so anything after age
            >> must also be optional.
            >>
            >> kmurphy
            >> roughly at nexusinfosys in the com domain.
            >>
            >> "Dragon" <baadil_nospam@ hotmail.com> wrote in message
            >> news:O$oWwG00FH A.2132@TK2MSFTN GP15.phx.gbl...[color=darkred]
            >>> Hi,
            >>>
            >>> Is there a way to set defaults values for args of a function/sub?
            >>>
            >>> For example:
            >>>
            >>> Function:
            >>> public function testFunc (name as string, age as integer = 30, phone as
            >>> string)
            >>>
            >>> Calling the Function:
            >>> testFunc("John" , 25, "555-1212")
            >>> testFunc("John" , , "555-1212") ' Age will be set to default of 30
            >>>
            >>> This way one or more of arg can be left out and still have a working
            >>> function/sub.
            >>>
            >>> Thank you.
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Default values for args

              "Dragon" <baadil_nospam@ hotmail.com> schrieb:[color=blue]
              > Is it not available in C#?[/color]

              No, it isn't. However, code written in C# can consume/call methods which
              have optional parameters.

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://classicvb.org/petition/>

              Comment

              • Dragon

                #8
                Re: Default values for args

                Hi,

                You can have optional arguments, but they must be the last ones in the
                list of arguments, e.g.:

                ~
                Public Function TestFunc (ByVal Name As String, ByVal Phone As
                String, Optional ByVal Age As Integer = 30) As BadImageFormatE xception
                ~

                I would recommend you to have multiple overloaded versions of function
                i.e.:

                ~
                Public Function TestFunc(ByVal Name As String, ByVal Phone As
                String) As BadImageFormatE xception
                Return TestFunc(Name, Phone, 30)
                End Function

                Public Function TestFunc(ByVal Name As String, ByVal Phone As
                String, ByVal Age As Integer) As BadImageFormatE xception
                REM Your code goes here...
                End Function
                ~

                HTH,
                Roman


                Comment

                • Mythran

                  #9
                  Re: Default values for args


                  "Dragon" <baadil_nospam@ hotmail.com> wrote in message
                  news:O$oWwG00FH A.2132@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > Hi,
                  >
                  > Is there a way to set defaults values for args of a function/sub?
                  >
                  > For example:
                  >
                  > Function:
                  > public function testFunc (name as string, age as integer = 30, phone as
                  > string)
                  >
                  > Calling the Function:
                  > testFunc("John" , 25, "555-1212")
                  > testFunc("John" , , "555-1212") ' Age will be set to default of 30
                  >
                  > This way one or more of arg can be left out and still have a working
                  > function/sub.
                  >
                  > Thank you.
                  >
                  >[/color]

                  I don't know why I don't like optional parameters...I think it was because
                  some other languages don't see those methods when accessing them via COM?
                  Anywho, a way around it would be:

                  Public Overloads Function TestFunc(ByVal Name AS String, ByVal Age As
                  Integer, ByVal Phone As String) As Whatever

                  End Function

                  Public Overloads Function TestFunc(ByVal Name As String, ByVal Phone As
                  String) As Whatever
                  TestFunc(Name, 30, Phone)
                  End Function

                  Public Overloads Function TestFunc(ByVal Name As String)
                  TestFunc(Name, 30, "555-1212")
                  End Function

                  Calls like:

                  TestFunc("John" , 30, "555-1212")
                  TestFunc("John" , "555-1212")
                  TestFunc("John" )

                  Use overloading to achieve what you want, similarly....

                  HTH,
                  Mythran

                  Comment

                  • Dragon

                    #10
                    Re: Default values for args

                    Thank you Mythran.



                    "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
                    news:e%23oGdk10 FHA.712@TK2MSFT NGP10.phx.gbl.. .[color=blue]
                    >
                    > "Dragon" <baadil_nospam@ hotmail.com> wrote in message
                    > news:O$oWwG00FH A.2132@TK2MSFTN GP15.phx.gbl...[color=green]
                    >> Hi,
                    >>
                    >> Is there a way to set defaults values for args of a function/sub?
                    >>
                    >> For example:
                    >>
                    >> Function:
                    >> public function testFunc (name as string, age as integer = 30, phone as
                    >> string)
                    >>
                    >> Calling the Function:
                    >> testFunc("John" , 25, "555-1212")
                    >> testFunc("John" , , "555-1212") ' Age will be set to default of 30
                    >>
                    >> This way one or more of arg can be left out and still have a working
                    >> function/sub.
                    >>
                    >> Thank you.
                    >>
                    >>[/color]
                    >
                    > I don't know why I don't like optional parameters...I think it was because
                    > some other languages don't see those methods when accessing them via COM?
                    > Anywho, a way around it would be:
                    >
                    > Public Overloads Function TestFunc(ByVal Name AS String, ByVal Age As
                    > Integer, ByVal Phone As String) As Whatever
                    >
                    > End Function
                    >
                    > Public Overloads Function TestFunc(ByVal Name As String, ByVal Phone As
                    > String) As Whatever
                    > TestFunc(Name, 30, Phone)
                    > End Function
                    >
                    > Public Overloads Function TestFunc(ByVal Name As String)
                    > TestFunc(Name, 30, "555-1212")
                    > End Function
                    >
                    > Calls like:
                    >
                    > TestFunc("John" , 30, "555-1212")
                    > TestFunc("John" , "555-1212")
                    > TestFunc("John" )
                    >
                    > Use overloading to achieve what you want, similarly....
                    >
                    > HTH,
                    > Mythran
                    >[/color]


                    Comment

                    Working...