# Operator\_Convert

**URL:** <https://ifnotnil.com/t/operator-convert/1533>\
**Category:** Other\
**Created:** [12 February 2021 15:56 UTC](https://ifnotnil.com/t/operator-convert/1533 "2021-02-12T15:56:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [12 February 2021 15:56 UTC](https://ifnotnil.com/t/operator-convert/1533/1 "2021-02-12T15:56:15Z")

</div>

From TOF

> **[Operator\_Convert Question](https://forum.xojo.com/t/operator-convert-question/61164)**
>
> Hi, I have a main class ClassA. From it there are two subclasses ClassB and ClassC. Each of these subclasses has different constructors. Now there is ClassTest with a Fill As ClassA property. How can I shorten the previous assignment from this...

@anon29821626

> [@](#):
>
> Hi,
> 
> I have a main class `ClassA`. From it there are two subclasses `ClassB` and `ClassC`. Each of these subclasses has different constructors.
> 
> Now there is `ClassTest` with a `Fill As ClassA` property.
> 
> How can I shorten the previous assignment from this code (MyPicture = Picture)
> 
> ```auto
> Var instance As New ClassTest
> 
> instance.Fill = New ClassB(Color.Red)
> ' or
> ' instance.Fill = New ClassC(MyPicture)
> 
> ```
> 
> to this notation using `Operator_Convert`?
> 
> ```auto
> Var instance As New ClassTest
> 
> instance.Fill = Color.Red
> ' or
> ' instance.Fill = MyPicture
> 
> ```
> 
> So far I get a syntax error. Maybe @Kem\_Tekinay?

Operator\_convert can be used in both a CONVERT TO and CONVERT FROM style  
The CONVERT TO style is one with a return type - not the one in use here

The convert FROM is more like a CONSTRUCTOR  
In this case you COULD make operator convert work like

```auto
Var instance As ClassTest

instance = Color.Red // creates a new instance with the Fill set to Color.Red
' or
instance = MyPicture // creates a new instance with the Fill set to MyPicture

```

Here’s [a quick sample](http://great-white-software.com/miscellaneous/explaining%20operator_convert.zip) of this in action

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [12 February 2021 17:31 UTC](https://ifnotnil.com/t/operator-convert/1533/2 "2021-02-12T17:31:24Z")

</div>

[https://www.great-white-software.com/blog/2020/03/30/operator\_convert-2/](https://www.great-white-software.com/blog/2020/03/30/operator_convert-2/)  
[https://www.great-white-software.com/blog/2019/12/05/operator\_convert/](https://www.great-white-software.com/blog/2019/12/05/operator_convert/)  
[https://www.great-white-software.com/blog/2021/02/09/convert-to-or-convert-from/](https://www.great-white-software.com/blog/2021/02/09/convert-to-or-convert-from/)

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [12 February 2021 22:01 UTC](https://ifnotnil.com/t/operator-convert/1533/3 "2021-02-12T22:01:56Z")

</div>

The other way to make this work is the use of operator\_lookup 😛

```auto
Class ClassTest
     sub operator_lookup(name as string, assigns v as variant)
          select case name
              case "Fill"
                   If (newValue.Type = Variant.TypeColor) Then
                      mFill = New ClassB(newValue.ColorValue)
                  Elseif newValue.Type = Variant.TypeObject And newValue IsA Picture Then
                      mFill = New ClassC(Picture(newValue.ObjectValue))
                  End If
            Else
               Break
         End Select
     end sub

     protected property mFill as ClassA
end class

```

and now you can write

```auto
Var instance As ClassTest

instance.Fill = Color.Red // creates a new instance with the Fill set to Color.Red
' or
instance.Fill = MyPicture //

```

BUT code like

```auto
Var instance As ClassTest
 // compiles & runs as we have appropriate code in operator_lookup for the Fill property
instance.Fill = Color.Orange

// compiles but DOES NOT RUN as operator_lookup has no code for this
// and the compiler WILL NOT complain or warn about no such property !
instance.foobar = 1

```

So BE CAREFUL with the use of operator\_lookup

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [12 February 2021 23:50 UTC](https://ifnotnil.com/t/operator-convert/1533/4 "2021-02-12T23:50:40Z")

</div>

Another way to solve this is method pairs (or maybe a computed property)

```auto
Class ClassTest
     sub Fill( assigns value as color )
        mFill = New ClassB(value)
     end sub
     sub Fill( assigns value as picture )
         mFill = New ClassC(value)
     end sub
     function Fill() as ClassA
        return mFill
     end function
    
     protected property mFill as ClassA
end class

```

which lets you do

```auto
Var instance As ClassTest

instance.Fill = Color.Red // creates a new instance with the Fill set to Color.Red
' or
instance.Fill = MyPicture //

```

and you will still get compile errors, type mismatch errors, etc

operator\_convert is not needed  
nor is operator\_lookup
