Operator_Convert is a very handy method to implement on a lot of classes. It not only lets you create a mechanism to make your classes convert themselves into other types but it also lets you create new instances FROM other data types.
So let’s start with the simple forms of operator_convert – the “convert TO” forms.
Suppose you have some class defined called “myClass”
Class myClass
End Class
and you want to make it easy to have this class write log messages to the DebugLog. You most certainly could write a “ToString” method as an explicit way to convert this class to a String
Class myClass
Function ToString() as string
End Function
End Class
and then in your code you’d use it like :
dim instance as new MyClass
// lots of intervening code
system.debuglog instance.ToString
And maybe this isn’t so bad. It IS explicit which is most times a good thing. Your code will have a certain clarity to it because of this style.
But there is another way to do much the same thing that is also effective – despite it being slightly hidden and magical like a variants ability to automatically convert itself to other data types by assignment. And this “magic” is operator_convert
In a class like our sample
Class myClass
Function Operator_Convert() as string
End Function
End Class
And now your usage could look like
dim instance as new MyClass
// lots of intervening code
system.debuglog instance
Since System.Debuglog takes a string as a parameter the compiler figures out that our class has a suitable Operator_convert method and will call that for us.
You can define many operator_convert functions that convert TO other datatypes and as long as they dont cause any ambiguity you will get the results you expect. You may get an ambiguous situation of you define a Convert To operator_convert for Integer, Int32 and/or Int64 since Integer is simply an alias to one of those types depending on whether you are building for 32 or 64 bit. There can be others but this is the most common.
There is another form for operator_convert – the “convert FROM” form.
In this case operator_convert behaves in many ways like a constructor called with a parameter. However one thing to remember is that Operator_convert is NOT a constructor! Its an initializer at best. If your class depends on constructors being called make sure when you use the convert FROM form you call your constructor !
For our sample class suppose we want to initialize it from a string.
Class myClass
Function Operator_Convert() as string
End Function
Sub Operator_Convert(initializeWith as string)
// if we rely on the constructor having been called
// then call the Constructor here !
// now set our class up from whatever data is in the
// parameter passed
End Sub
End Class
and we could use it like
dim instance as MyClass = "123"
// lots of intervening code
system.debuglog instance
Something to note is that these conversions can appear to be hidden away from a casual reader so use them with care and consideration. Often explicit code, like the ToString example above aids a reader in understanding the code because its right there in front of them that you are asking the class to give you a string representation for some purpose.
Keep that in mind and use these with that in mind.
Enjoy !