CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2000
    Posts
    14

    Need help with optional function parameters!

    I have a function that takes a list of 4 parameters. Only 1 of these are needed and the function executes differently depending on the other 3 parameters. ie:
    If parameter2<>"" then
    do this
    end if
    if parameter3<>"" then
    do this
    end if
    and so on...but when i call the function:
    MyFunction(1,,"hello")
    I get an error saying Parameter2 doesn't exist. How can i check to see if parameter2 has a value? I tried isNull and that didn't work either. Thanks for your help!
    -Aaron



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Need help with optional function parameters!

    In your function type the word 'Optional' in fromt of your optional parameter

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Nov 2000
    Posts
    14

    Re: Need help with optional function parameters!

    I do that but I still get the error (sorry, i should have mentioned that)
    -Aaron


  4. #4
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Need help with optional function parameters!

    There are two methods to it. If it doesn't matter if the arguments are variant, then you can use the IsMissing() function to check if the parameter was passed.


    private Sub Func1(Param1, optional Param2, optional Param3, optional Param4)
    If Not IsMissing(Param2) then
    ...
    ...
    End If
    ... and so on
    End Sub




    If instead, your variable cannot be variant, you can set it in the declaration to a value that the variable doesn't typically contain as follows. Then in the function, check if the variable has that atypical value.


    private Sub Func2(Param1 as Integer, optional Param2 as Integer = -999, optional Param3 as string = "", optional Param4 as Boolean = true)
    If Not (Param2 = -999) then
    ...
    ...
    End If
    ... and so on
    End Sub







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

Featured