Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optional parameters in a property procedure 1

Status
Not open for further replies.

joxo1

Programmer
Aug 5, 2004
25
0
0
GB
Hi,

I am tring to use an optional parameter in a property let procedure. e.g

Property Let Caption(byval strValue as string, optional InBold as varient)

'-------

End property

I keep getting the error "Argument not optional". All the documentation i can find says that this should work.

Can anyone shed any light?

Cheers

 
Think about the syntax used for setting a property (from the outside of the class!


Using your example above,
Code:
myObj.Caption = "My Caption"   ' Now where do we add the In bold argument?

You simply can't do this as a property Let/get pair
The conventional way that you can do this is to have a Public Sub (maybe SetCaption) that takes both arguments.

such as

Code:
Public Sub SetCaption(byval Mystring as string, optional inBold as boolean = False)

mstrCaption = Mystring
mblnInBold = inBold
end sub

public property get Bold() as Boolean
etc




Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I think you have slightly misunderstood the documentation. The variable you are passing in to the Let procedure must be the last parameter, and cannot be optional. So, guessing what yoiu are trying to do, I suspect your Let statement should actually read:

Property Let Caption(Optional InBold as Variant, ByVal strValue as String)

oh, and if you have a Get statement, you need to make sure it matches...something like (otherwise you'll get compile time error):

Property Get Caption(InBold as Variant) as String
 
>Think about the syntax used for setting a property
>You simply can't do this as a property Let/get pair

You can, you know...
 
(er...not that I'm suggesting that it is necessarily good practice)
 
>You can, you know...

No I didn't...


But I do now!

Just to complete my knowledge....

how would you call such a property let and get pair?

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
OK. Create a class called Class1. Drop in the following code:
Code:
Option Explicit

Property Let Caption(Optional InBold As Variant = False, ByVal strValue As String)
    ' Pointless stuff, but useful to illustrate
    If InBold Then
        MsgBox "BOLD " & strValue
    Else
        MsgBox strValue
    End If
        
End Property

Property Get Caption(InBold As Variant) As String
    Caption = InBold ' Silly, but just for sake of example
End Property
Now you need a form and a command button:
Code:
Option Explicit

Private Sub Command1_Click()
    Dim a As Class1
    Dim dummy As Long
    
    Set a = New Class1
    
    a.Caption = "hello"
    a.Caption(True) = "hello"
    MsgBox a.Caption("wombat")
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top