# Operator\_Convert Question

**URL:** <https://forum.xojo.com/t/operator-convert-question/61164>\
**Category:** General\
**Created:** [February 12, 2021, 3:05pm UTC](https://forum.xojo.com/t/operator-convert-question/61164 "2021-02-12T15:05:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 12, 2021, 3:05pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/1 "2021-02-12T15:05:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Andrew\_Lambert](https://forum.xojo.com/user_avatar/forum.xojo.com/andrew_lambert/32/215_2.png) [@Andrew\_Lambert](https://forum.xojo.com/u/Andrew_Lambert)\
**Post date:** [February 12, 2021, 4:09pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/2 "2021-02-12T16:09:42Z")

</div>

Use the [Assigns](http://documentation.xojo.com/api/code_execution/assigns.html) keyword instead of Operator\_Convert. Make one method per datatype:

```
Sub Fill(Assigns c As Color)

Sub Fill(Assigns p As Picture)

```

etc.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 12, 2021, 4:10pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/3 "2021-02-12T16:10:00Z")

</div>

If Fill is a ClassA but you want to force a ClassB or ClassC, you can’t use Operator\_Convert.

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 12, 2021, 4:16pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/4 "2021-02-12T16:16:24Z")

</div>

Why? They are both Subclasses. ☹

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 12, 2021, 4:17pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/5 "2021-02-12T16:17:46Z")

</div>

Good suggestion @Andrew_Lambert, thank. Since I have a couple of classes which habe ClassA Properties, this won’t be handy.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 12, 2021, 4:19pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/6 "2021-02-12T16:19:01Z")

</div>

```auto
Fill As ClassA

```

ClassA has `Operator_Convert(c As Color)`, so when you assign the Color to Fill, you will get an instance of ClassA. If you want an instance of ClassB, you’d have to do this:

```auto
var b as ClassB = Color.Red
instance.Fill = b

```

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 12, 2021, 4:20pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/7 "2021-02-12T16:20:53Z")

</div>

> [@Kem\_Tekinay](#):
>
> when you assign the Color to Fill, you will get an instance of ClassA. If you want an instance of ClassB, you’d have to do this:

Which then in my case is almost the same as `instance.Fill = New ClassB(Color.Red)`. Thanks.

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [February 12, 2021, 11:25pm UTC](https://forum.xojo.com/t/operator-convert-question/61164/9 "2021-02-12T23:25:00Z")

</div>

Another solution could be this.

Thanks to @Norman_Palardy. We can get it work the way I want adding the following `Operator_Lookup` to `ClassTest`:

```auto
Sub Operator_Lookup(name As String, Assigns newValue As Variant)
  Select Case name
  Case "Fill"
    If (newValue.Type = Variant.TypeColor) Then
      Fill = New ClassB(newValue.ColorValue)
    Elseif newValue.Type = Variant.TypeObject And newValue IsA Picture Then
      Fill = New ClassC(Picture(newValue.ObjectValue))
    End If
  End Select
End Sub

```

`ClassB` and `ClassC` need to get an `Operator_Convert` method. That’s all.

> **Warning:** `Operator_Lookup` overrides the compilers checking in a way that you have to really know what you are doing as a lot of compiler error will not occur any more!
