In a recent discussion on the forums someone said that “operator_convert acts like a constructor”.

It’s true, except when its not.

For instance, if you create a class that has an “operator_convert from” then you may be surprised at the behaviour.

So first, whats an “operator_convert from” ?

A class can have two forms of operator_convert. One takes a parameter. And one doesnt. For instance if I have a class, Box, that can be created from a string there are several ways to do this.

You might write one that is a constructor that takes a string that holds the top, left, width and height

Sub Constructor(constructFromString as String)

But then your code has to do something like

Dim b as new Box( "0,0,10,10" )

But this isnt always quite as convenient as we’d like. One other option is to provide the “operator_convert from” form of Operator_convert. That would be a nethod you add that looks like

Sub operator_convert(constructFromString as String)

And this form lets you write code that looks like

Dim b as Box =  "0,0,10,10" 

This appears a little nicer. So yes, in this case operator_convert from acts a LOT like a constructor.

However, if your constructor sets some internal state that the instance requires note that Operator_Convert does NOT call the constructor at all.

You can see this in action if, in th case of the Box class above, we put a break statement in the Constructor and then use the operator_convert from form.

Class Box
   Sub Constructor(constructFromString as string)
   End Sub
   Sub Constructor()
   End Sub
   Sub Operator_convert(constructFromString as string)
   End Sub
End Class

dim b as Box = "10,10,50,50"

You’ll find that you DO get a new instance BUT neither constructor is called. so IF you rely on the fact that your constructor HAS configured some properties in a particular way you might need to manually call the constructor yourself in the Operator_Convert from Form.

You can overload operator_convert from and convert from as many types as you want for your. needs

The second form of operator_convert is the “operator_convert to” form. This one is code you can use to take a custom class and return a different representation of the class..You might want to return it as a string, a double, or just about any other form you might need.

In our box example above the operator_convert to form might return a string. It might look like

Sub operator_convert() as String

Note this form never takes a parameter. And you can have as many as you want as long as they do not cause any ambiguity. If you add one that converts to integer then don’t try and add one for int32 and int64 since integer is either an int32 or and int64 as you will get compilation errors.

Both forms of operator_convert are VERY handy.

3 Replies to “Operator_convert”

  1. > Dim b as Box = “0,0,10,10”

    Wouldn‘t that be Dim b as New Box = “0,0,10,10” ?

    I think that looks worse.

    Then if the operator_convert acts as a Constructor and you need the Constructor to do additional stuff, then naturally you have to add that or call the existing Constructor.

    Or am I missing something here?

    1. No it wouldn’t (really I encourage you to try it to satisfy your curiosity)
      In fact writing it that way would be a syntax error 🙂

      Thats the point here
      Operator_convert acts LIKE a constructor
      But its not a constructor nor does it call any you write. AT least not automatically like NEW does.

      If you rely on a constructor being called for ALL instances then the CONVERT FROM forms _should_ call which ever constructor you have that is suitable. Nice thing is that it can be a protected one if need be since the Operator_convert IS part of the class and can call protected methods.

Comments are closed.