In this particular case, it is str, but I wait to make the function generic so the calling routine can do anything with the return value. Is there a way to force the Operator_Convert to always return an integer?
Or forget Operator_Convert and just add a Result (or similar) function or Computed Property (useful for debugging).
I would go with what @Kem Tekinay suggests
You can also use ctype if you really want to
TextField_Result.Text = Str (ctype(p,integer))
But honestly, I would rethink this… I’m thinking of a future situation where you store the class in an integer property and then the instance goes out of scope because you’ve only stored the result rather than the instance…
using a .result computed property is a very good suggestion.
I’m missing something. Instead of a .result computed property, I should just use a result method, as I posted originally in this thread (I called it . Action)
The idea of using a computed property is so that when you are looking at the debugger values pane, you’ll see the value. If it is a method, you don’t see it in the debugger.
What Jim said. There are some other differences like a method can be overridden whereas a property will be shadowed, but will otherwise perform the same function in this context.
Thanks for the clarification - that was a subtlety that eluded me.
Well, I don’t think it is a great idea to start using dictionaries as method parameters because you loose a lot of the type checking, auto complete, help tips etc…
But if you are going to do it and you want to make it look pretty and more legible then I’d suggest you create a class which encapsulates the dictionary and have that class implement the Operator_Lookup operator. You could then write code like:
Dim items As New MyParamsList
items.X = 1
items.Name = "Jim"
items.MyFolder = SpecialFolder.Documents
Call MyFuncX(items)
Dim newItems As NewParamsList
newItems.Y = "ABC"
newItems.MyColor = Color.Red
Call MyOtherFunc(newItems)
You still will loose out on the auto complete and help prompts. The example code below shows you how to do this.
Sub Operator_Lookup(key As String, Assigns value As Variant)
//--------------------------------------------------------------------------------
// Sets or updates the value for a property
//
Dim actualKey As String = PropertyToKey(key)
Dim item As SetupItemBase
If mList.HasKey(actualKey) Then
item = SetupItemBase(mList.Value(actualKey))
item.Value = value
Return
End If
Dim type As Integer = VarType(value)
Select Case type
Case 2, 3
item = New IntegerSetupItem(actualKey, value)
Case 4
item = New SingleSetupItem(actualKey, value)
Case 5
item = New DoubleSetupItem(actualKey, value)
Case 6
item = New CurrencySetupItem(actualKey, value)
Case 7
item = New DateSetupItem(actualKey, value)
Case 8
item = New StringSetupItem(actualKey, value)
Case 9
item = New FolderItemSetupItem(actualKey, value)
Case 11
item = New BooleanSetupItem(actualKey, value)
Case 16
item = New ColorSetupItem(actualKey, value)
Case Else
SetupException_AS.Throw(kError_DataTypeNotSupported + Str(type))
End Select
mList.Value(actualKey) = item
End Sub
Function Operator_Lookup(key As String, Optional value As Variant) As Variant
//--------------------------------------------------------------------------------
// Returns the value of the property, with an optional
// default
//
Dim actualKey As string = PropertyToKey(key)
Dim item As SetupItemBase
If mList.HasKey(actualKey) Then
item = SetupItemBase(mList.Value(actualKey))
Return item.Value
End If
Return value
End Function
Thanks to all who helped out. I tried both the Dictionary method and the Class method with a Class.Result function that returns the result. I was not concerned about the Calculated Property because I use a system log that reports the returned value of functions (substantially better than breakpoints).
In the end, I chose the Class method, because I also define Constants within the Class that I can use as Integer Enumerators, as in Class.Property = Class.Constant. It makes the code very clean and easy to use, especially with IDE type look-ahead for both property and constant names within the class.