[Gambas-user] Use a class as a function
Gianluigi
bagonergi at gmail.com
Fri Dec 18 22:16:50 CET 2020
Hi,
could this example help to understand _call() method and also the "..."
keyword?
[code]----------------------------------------
' Gambas module file
Public Sub Main()
Dim Operations As New CMath
Print Operations(Operations.Addition, 1.2, 2.0, 3.55, 4.0, 5.12)
Print Operations(Operations.Subtraction, 12345.02, 45.5, 67.31, 890.12,
543.0, 987.09, 345.13, 123.56, 4567.0)
Print Operations(Operations.Multiplication, 32, 64, 12, 21)
Print Operations(Operations.Division, 1234, 56, 78, 93, 4)
End
---------------------------------------------
' Gambas class file
Public Enum Addition = 1, Subtraction, Multiplication, Division
Public Sub _call(Operator As Integer, ...) As Variant
Dim v As Variant = Param.All
Dim i, iType As Integer
Dim vResult As Variant = 0
If Param.Count = 0 Then Return
iType = TypeOf(v[0])
If iType = 4 Then
CLong(vResult)
Else If iType = 7 Then
CFloat(vResult)
Else
Debug "#Warning: Numbers must be entered"
Return
Endif
For i = 0 To Param.Max
If TypeOf(v[i]) <> iType Then
Debug "#Warning: Impossible to operate on different types of data"
Return
Endif
Next
Select Case Operator
Case 1 ' Addition
For i = 0 To Param.Max
vResult += v[i]
Next
Return vResult
Case 2 ' Subtraction
vResult = v[0]
For i = 1 To Param.Max
vResult -= v[i]
Next
Return vResult
Case 3 ' Multiplication
vResult = v[0]
For i = 1 To Param.Max
vResult *= v[i]
Next
Return vResult
Case 4 ' Division
vResult = v[0]
For i = 1 To Param.Max
Try vResult /= v[i]
If Error Then
Debug "#Error :"; Error.Text
Return
Endif
Next
Return vResult
End Select
End
[/code]----------------------------------------
Thank you and regards
Gianluigi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20201218/fa89c6f6/attachment.htm>
More information about the User
mailing list