Hey,
I want a function with an optional UDT parameter.
I could use overload, witch is a superb concept, but in this particular case I would prefer the optional way.
From you guys, is this the better way to do it?
I mean by using "Common BasketEmpty As Fruit" to set the filling UDT.
Thank,
Pierre
#Include Once "windows.bi"
TYPE Fruit
Banana As WORD
Clementine As DWORD
Coconut As DOUBLE
Lime As WORD
End type
Common BasketEmpty As Fruit '=> {2, 5, 1.5, 2} 'COMMON variables cannot be initialized
Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit = BasketEmpty) As DWORD
Print Basket.Banana
End Function
Dim Basket As Fruit
Basket.Banana = 2
DoGrocery("Store", Basket)
DoGrocery("Store")
Print "Press a key or click to end"
Dim buttons As Long
Do
GetMouse(0, 0, 0, Buttons) : IF buttons Or Len(InKey) Then Exit Do : Sleep 200
Loop
I think that the best way is
Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit PTR = NULL) As DWORD
IF Basket then Print Basket->Banana
> Common BasketEmpty As Fruit '=> {2, 5, 1.5, 2} 'COMMON variables cannot be initialized
But if the purpose is to have default values, then you can use:
Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit = TYPE(2, 5, 1.5, 2)) As DWORD
Print Basket.Banana
You can also use a define, e.g.
#define BasketEmpty type(2, 5, 1.5, 2)
Function DoGrocery(ByVal sStore As String, ByVal Basket As Fruit = BasketEmpty) As DWORD
Print Basket.Banana
Superb,
#define BasketEmpty will be great for now, being enable to initialize give more power.
"= TYPE()" seems also pretty good and versatile...
and
"PTR = NULL" will be surely very usefull some time later.
I really like FB more and more.
Thank you Jose, great answer. :-)
Pierre
A potpourri made with Jose's recipies...
'Console
#Define unicode
#Include Once "windows.bi"
Type Fruit
Banana As WORD
Clementine As DWORD
Coconut As Double
Lime As WORD
Lemon As String * 15
End Type
Common BasketEmptyCommon As Fruit '=> {2, 5, 1.5, 2} 'COMMON variables cannot be initialized
#Define BasketPreFilledDefined Type(7, 22, 1.1, 3, "Yellow")
Function DoGroceryPointer(ByVal sStore As String, ByVal Basket As Fruit Ptr = NULL) As DWORD
If Basket Then
Print "[" & Basket->Banana & "]"
Print "[" & Basket->Lemon & "]"
Else
Print "[No banana]"
Print "[No lemon color]"
EndIf
End Function
Function DoGroceryType(ByVal sStore As String, ByVal Basket As Fruit = Type(2, 5, 1.5, 2, "Light yellow")) As DWORD
Print "[" & Basket.Banana & "]"
Print "[" & Basket.Lemon & "]"
End Function
Function DoGroceryCommon(ByVal sStore As String, ByVal Basket As Fruit = BasketEmptyCommon) As DWORD
Print "[" & Basket.Banana & "]"
Print "[" & Basket.Lemon & "]"
End Function
Function DoGroceryDefine(ByVal sStore As String, ByVal Basket As Fruit = BasketPreFilledDefined) As DWORD
Print "[" & Basket.Banana & "]"
Print "[" & Basket.Lemon & "]"
End Function
Dim Basket As Fruit
Basket.Banana = 22
Basket.Lemon = "Green"
Print "#Define"
DoGroceryDefine("Store", Basket)
DoGroceryDefine("Store")
Print
Print "Common"
DoGroceryCommon("Store", Basket)
DoGroceryCommon("Store")
Print
Print "Type"
DoGroceryType("Store", Basket)
DoGroceryType("Store")
Print
DoGroceryPointer("Store", @Basket)
DoGroceryPointer("Store")
Print "Press a key or click to end"
Dim buttons As Long
Do
GetMouse(0, 0, 0, Buttons) : If buttons Or Len(Inkey) Then Exit Do : Sleep 200
Loop
If you want to type less, instead of
DoGrocery4("Store", VarPtr(Basket))
you can use
DoGrocery4("Store", @Basket)
Contrarily to PowerBASIC, @ does not deference a pointer (FB uses * for that), but it is a shortcut for Address Of and can replace both VARPTR and PROCPTR. It even works with string literals.
Yep, I forgot to use @, this was an old reflex, code corrected.
Thank,
Pierre