Results 1 to 1 of 1

Thread: Optional default-value & IsMissing(argument)

  1. #1
    thinBasic MVPs
    Join Date
    Oct 2012
    Rep Power

    Lightbulb Optional default-value & IsMissing(argument)

    IsMissing is Keyword working for pb and vb that simply tells if an optional parameter was passed or not.
    The byref way works at least for strings alike

    ...opt byref sData as string,...

    If VarPtr(sData) then
    ' there is a strptr!
    ergo IsMissing(sData) will be False

    I am not sure if the optional default parameter-values were fixed in the meantime - i did not use them for years now.

    And an interesting way that is certainly more useful then Function_CParams which sometimes does not return the correct count of passed arguments that is implemented to vb6 - (the syntax reminds of AutoIt/AutoHotKey/Relax and others using ":=" which were probably also an option for cHash to let remain empty buckets instead of confusing the colon with the End-Of-Line symbol
    (btw. help wrongly mentions "semicolon" ( multiple times there where it should be "colon"(

    I just paste the vb6-way here
    - of course it were nonsense to copy the need of keyword OPTIONAL for every optional parameter

    Quote Originally Posted by MSDN Help for vb6.0
    Using Optional Arguments

    You can specify arguments to a procedure as optional by placing the Optional keyword in the argument list. If you specify an optional argument, all subsequent arguments in the argument list must also be optional and declared with the Optional keyword. The two pieces of sample code below assume there is a form with a command button and list box.
    For example, this code provides all optional arguments:
    Dim strName As String
    Dim strAddress As String

    Sub ListText(Optional x As String, Optional y _
    As String)
    List1.AddItem x
    List1.AddItem y
    End Sub

    Private Sub Command1_Click()
    strName = "yourname"
    strAddress = 12345 ' Both arguments are provided.
    Call ListText(strName, strAddress)
    End Sub
    This code, however, does not provide all optional arguments:
    Dim strName As String
    Dim varAddress As Variant

    Sub ListText(x As String, Optional y As Variant)
    List1.AddItem x
    If Not IsMissing(y) Then
    List1.AddItem y
    End If
    End Sub

    Private Sub Command1_Click()
    strName = "yourname" ' Second argument is not
    ' provided.
    Call ListText(strName)
    End Sub
    In the case where an optional argument is not provided, the argument is actually assigned as a variant with the value of Empty. The example above shows how to test for missing optional arguments using the IsMissing function.
    Providing a Default for an Optional Argument

    It's also possible to specify a default value for an optional argument. The following example returns a default value if the optional argument isn't passed to the function procedure:
    Sub ListText(x As String, Optional y As _
    Integer = 12345)
    List1.AddItem x
    List1.AddItem y
    End Sub

    Private Sub Command1_Click()
    strName = "yourname" ' Second argument is not
    ' provided.
    Call ListText(strName) ' Adds "yourname" and
    ' "12345".
    End Sub

    Creating Simpler Statements with Named Arguments

    For many built-in functions, statements, and methods, Visual Basic provides the option of using named arguments as a shortcut for typing argument values. With named arguments, you can provide any or all of the arguments, in any order, by assigning a value to the named argument.
    You do this by typing the argument name plus a colon followed by an equal sign and the value

    MyArgument:= "SomeValue"

    and placing that assignment in any sequence delimited by commas.
    Notice that the arguments in the following example are in the reverse order of the expected arguments:

    Function ListText(strName As String, Optional strAddress As String)
    List1.AddItem strName
    List2.AddItem strAddress
    End Sub

    Private Sub Command1_Click()
    ListText strAddress:=12345, strName:="Your Name"
    End Sub

    This is especially useful if your procedures have several optional arguments that you do not always need to specify.
    Last edited by ReneMiner; 29-03-2024 at 21:08.
    I think there are missing some Forum-sections as beta-testing and support

Similar Threads

  1. TBGL module from Petr Schreiber Argument - card game
    By ReneMiner in forum TBGL Scripts and Projects
    Replies: 1
    Last Post: 08-04-2014, 18:39
  2. are Constants as Optional Default-Parameters invalid?
    By ReneMiner in forum thinBasic General
    Replies: 2
    Last Post: 18-05-2013, 20:24
  3. Problems / UI / TRACKBAR has no default styles
    By Petr Schreiber in forum Preview testing
    Replies: 1
    Last Post: 03-09-2008, 21:28
  4. Some default data
    By ErosOlmi in forum thinBundle suggest new features
    Replies: 0
    Last Post: 21-02-2007, 00:53

Members who have read this thread: 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts