Operator overloading problem

You should look into XojoScript, it’s designed for what you seem to be trying to accomplish.

[quote=205849:@Gerard Van Zee]The problem is that I need to preserve the ID property when I execute:

C = 2[/quote]
Right
In your existing code do
Dim C as Class1
C = 2
C.ID = 1

the ORDER is important because the operator_convert DOES create an instance

IF you treat it like its the same as using a constructor then
Dim C as Class1 = 2
C.ID = 1
or
Dim C as new Class1( 2 )
C.ID = 1

end up acting as you expect

that’s all I’m suggesting because = does NOT and quite probably never will work like you’re suggesting because it already has well defined semantics for using opertaor_convert
Changing it to act like you’re describing would break existing code which we try really hard to not do

Failing that just do

Dim C as new Class1
C.X= 2
C.ID = 1

and be done with it

There used to be a Default class property in bad old VB6. You could define X as the default property of Class1. And then
Dim A as new class1
Dim B as new class1
Dim C as new class1

C = 2 + C/A + B^A

works exactly to my needs. I’m just trying to achieve the same behavior in Xojo for a revived project. With overrides I have come to the point that I have the above line working, except for the = assignment But I have already come to the understanding that this is most likely not going to happen. I will need some alternative approach.

Again, thanks for your help. Highly appreciated.

That syntax doesn’t exist in the fashion you describe - Xojo is not VB
But, IF that 4 lines is the code you indicate

Dim A as new class1  // you get a new instance with default values for ALL properties 
Dim B as new class1  // you get a new instance with default values for ALL properties 
Dim C as new class1  // you get a new instance with default values for ALL properties 

C = 2 + C/A + B^A // C B and A have nothing BUT default values in any properties so recreating C harms it in NO way
                                // because it only has default values
                                  

If all you want is to assign to some specific property of C then just write

C.X = 2 + C/A + B^A

and you wont create a new instance

Minor syntax changes should enable you to revive your VB code

Yeah, it’s not going to be a stopper. Nonetheless, I do think my Operator_Assign override is a good idea.

So I’m off to bed. It’s past 2 am in central Europe.

Have a nice day. Gerard

Operator Assigns already exists - that literally what operator convert is - it just behaves differently than you desire and has a different name.

As Norman said, it does already exist: Operator_Convert.

Default Property in Visual Basic has nothing to do with operators or operator overriding, in fact it hasn’t even anything to do with assignment statements. Default Property in Visual Basic is just syntactic sugar.

That’s a bit like: Apple juice is Coca Cola, it just has a different name and tastes differently.

Gerard: the thing is that what you are looking for has nothing to do with operators. Operator_Convert is exactly the same as your proposed Operator_Assigns: the right-hand-side of the assignment statement is assigned to the left-hand-side. Look at the code you wrote:

Dim C as New Class1 // C ist the first instance of Class1 C.ID = 1 // C is the first instance C = 2 // C is now the second instance created, and instance 1 is now Nil
It is the same as this (as others have already mentioned above):

Dim C as New Class1 C.ID = 1 C = New Class1 C.X = 2

If Xojo would implement a default property along VB, it would most probably be with an Attribute you could set in the IDE (like “Hidden” for methods which should not show up in autocomplete).

Eli, I know that Operator_Convert works like that. But it does what I don’t want. I need an operator override that preserves the class instance and assign a value (or copies a class instance into a new class instance and then assigns the value to the copy), such as I suggested with Operator_Assign. Like this:

Dim C as New Class1 // C ist the first instance of Class1
C.ID = 1 // C is the first instance
C = 2 // C remains the first instance created, and now C.X equals 2 and C.ID equals 1

Whether this is achieved through on operator overload, a default property, or some other method: I don’t really care, as long as it serves my requirements. I would be highly surprised if Xojo were to decide to implement a new language feature for this issue. So I’ll find another way or accept the ugly syntax (ugly within the context of one specific project).

The problem with your operator_assign is the compile would have no way to know whether you meant operator_convert or operator_assign because they use the same syntax.

[quote=205941:@Gerard Van Zee]Dim C as New Class1 // C ist the first instance of Class1
C.ID = 1 // C is the first instance
C = 2 // C remains the first instance created, and now C.X equals 2 and C.ID equals 1[/quote]

But how would that work when passing as a method argument?
Say you have a method DoSomethingToSomeClass1Objects(c1 as class1, c2 as class1, c3 as class1)

With operator_convert you could call it as:

DoSomethingToSomeClass1Objects(5, 23, myOtherClass1Object)

But it this doesn’t really make sense with what you want to do with your operator_convert.

I personally use operator_convert as I have Nullable wrapper classes of all the primitives.
myClass.myProperty as NullableInteger
I can then do

myClass1.myProperty = 5 //instead of myClass1.myProperty = new NullableInteger(5)
and use the build in integer functions:

dim myMaxValue as integer = Max(5, myClass1.myProperty) //Assuming myProperty isn't null

The real use for operator_convert is to allow an implicit cast. What your trying to do is something completely different.

[quote=205972:@Brock Nash]
The real use for operator_convert is to allow an implicit cast. What your trying to do is something completely different.[/quote]
Its NOT a CAST of any kind
Operator convert allows you to convert FROM

sub operator_convert( param as type) // this is the convert FROM form // which WILL create a new instance (not cast an instance to another form) end sub

or CONVERT TO

function operator_convert() as type // this is the convert TO form end function

Neither is a cast - like this

dim i as Uint32 = Uint32(123)

[quote=205978:@Norman Palardy]Its NOT a CAST of any kind
Operator convert allows you to convert FROM or CONVERT TO[/quote]

Definition of cast:

http://www.yourdictionary.com/casting

I would argue this is a type of casting.

https://en.wikipedia.org/wiki/Cast
Cast, in computer science, is to change the interpretation of the bit pattern representing a value, from one data type to another

Not whats done with operator_convert in any way

but whatever

Cast changes the way the contents are interpreted, it doesn’t change the contents themselves. Cast is like Bruce Jenner changing his name. It says, treat me like a woman, but it doesn’t make him one.