CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2001
    Location
    india
    Posts
    50

    Problem with optional parameter

    Hi Vb Experts
    I have a problem,Please try to help me if you can.
    I am building an activex dll interface for a third party application. In doing so I am coding for some methods which have optional parameters.In some of the methods there are more than six to seven optional parameters. So when I make calls to those methods I have to check whether a particular parameter has been passed or not and then make a call to that method accordingly. In cases where there are a large number of optional parameters it becomes too tedious to check for all the combinations(for ex.The clent may paass the 2nd and 3rd parameter and may not pass the 4th and fifth parameter or it may be the other way round).
    Is there a way out in which I can avoid such lengthy coding.(Other than declaring the parameters as variant)
    Regards
    Sravanthi



  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Problem with optional parameter


    (If you already know I beg your pardon for loosing your time)
    This is the normal way:
    When you declare an optional parameter, you can check if it has been passed with the IsMissing clause
    ie:

    public sub mysub(optional parm1 as string, optional parm2 as integer, optional parm3 as long,...)
    if not ismissing parm1 then
    ...
    end if
    if not ismissing parm2 then
    ....
    end if
    ...
    end sub





    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    May 2001
    Location
    Canada
    Posts
    182

    Re: Problem with optional parameter

    IsMissing function will only work for Variant data type. As for others, like String, Integer,Long, IsMissing will never be True.

    You can set a default value for each optional parameter, but I still have no more idea on how to save codings.

    Regards,

    Michi

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    You're right

    Gurus next generation:
    Kdev, michi, Ghost306, Clearcode, Berta,....
    ;-)

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    May 2001
    Location
    india
    Posts
    50

    Re: You're right

    Hi
    Thank you for the suggestions.But my problem is still not solved.
    I a familiar with both the ways.But they do not help me since I can neither declares my variables as variant nor can I give them default values.Since if I give default values also I have to check the values of all such variables in different combinations.Which would again be tedoius.I want to know if there is a way to avoid such lengthy coding?
    Regards
    Sravanthi


  6. #6
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Problem with optional parameter

    Have you considered making the entire parm list a single structure?


  7. #7
    Join Date
    Sep 2001
    Posts
    4

    Re: Problem with optional parameter


    No there is no way to handle the problem u have stated . Once u have Optional parameter u need to check using IsMissing() for all the case .
    There is no alternative way for that .
    Cos me too faced the similar problem and i had 4 parameters .


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