Results 1 to 10 of 10

Thread: Function parameter

  1. #1
    DaoK
    Guest

    Function parameter

    I want to make a Function who not all parameter are needed how can I do that ?

  2. #2
    DaoK
    Guest
    Example :

    VB Code:
    1. Private Function Calcu(x as Double, y as Double, z as Double) as Double
    2.      Calcu = x + y + z
    3. End Function

    Like if I do not put z it will work... How can I do that ?

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     MsgBox Calcu(1, 2)
    5. End Sub
    6.  
    7. Private Function Calcu(x As Double, y As Double, Optional z As Double) As Double
    8.      Calcu = x + y + z
    9. End Function

  4. #4
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Orlando
    Posts
    392

    Use the optional keyword

    As follows:
    VB Code:
    1. Function XYZ(X As This, Y As That, [COLOR=red]Optional[/COLOR] Z As Whatever)
    2.     'Your Code
    3. End Function

    Thats it, you won't have to pass z if you don't want to.
    Remember the following points in case of using Optional Parameters:
    • The Paramebers following the first Optional Parameter must also be optional
    • You can specify the default value of the Optional Parameter, if the user doesn't Pass any Value for that
    • In case of a Variant Optional Parameter, you could check to see if it was specified using the IsMissing() Function
    • In case you don't specify the Default Value of other DataTypes, it will be initialized as a standard Variable of that type: Inetegers will be Initialized to zero
    • In case you need to use *any* number of Parameter which is unknown at the time you write the code, you can use the ParameterArray which would allow you to pass any number of parameters to a function.


    Cheers
    Abu Haider
    ____________________________
    100% Data Validation for the MS DataGrid Control. Plus Support for Custom and Foreign Lists, DatePicker and much more...
    The DataGridEnhancer


    I often point to a place where the problem has been discussed, instead of giving you the code that solves it. This is for good, may be you will understand some day...

  5. #5
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Orlando
    Posts
    392
    I think 50 words per minute is not good enough. I have to be faster.

    Abu Haider
    ____________________________
    100% Data Validation for the MS DataGrid Control. Plus Support for Custom and Foreign Lists, DatePicker and much more...
    The DataGridEnhancer


    I often point to a place where the problem has been discussed, instead of giving you the code that solves it. This is for good, may be you will understand some day...

  6. #6
    DaoK
    Guest
    Abu haider your explication was simply excellent!!! THX YOU man !!!

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Location
    Orlando
    Posts
    392
    Abu Haider
    ____________________________
    100% Data Validation for the MS DataGrid Control. Plus Support for Custom and Foreign Lists, DatePicker and much more...
    The DataGridEnhancer


    I often point to a place where the problem has been discussed, instead of giving you the code that solves it. This is for good, may be you will understand some day...

  8. #8
    DaoK
    Guest
    The only one thing I do not understand is : In case you need to use *any* number of Parameter which is unknown at the time you write the code, you can use the ParameterArray which would allow you to pass any number of parameters to a function.


  9. #9
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258
    If you dont know the datatype of the para

  10. #10
    DaoK
    Guest
    Yes but I do not know what to do in that case

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