Implicit constructor

Is there a way to have a default value for a custom class similar to what xojo primitives do? Like an implicit default constructor?
For example, lets say myCustomClass is a wrapper for integer and has operator_add, _addRight and _convert(value as integer) implemented.

dim mcc as myCustomClass
dim ii as integer = 14 + mcc

Is there a way to get the code above to work without a nilobjexception and without explicitly stating that mcc is new myCustomClass = 0?

A usage would be to have these as properties of other classes that do not need to be explicitly instantiated to a default value.

Write a constructor method with no parameters that sets the value of itself to = 0.

wouldn’t that still need to get called via the keyword new?

Using that method, yes.

Dim mcc as New myCustomClass

No. Classes are objects. If you don’t create an instance of the object, you will get a NOE.

[quote=416610:@erin jamroz]
dim mcc as myCustomClass
dim ii as integer = 14 + mcc[/quote]
If mcc isn’t dynamic, then you want a Constant here.

I think you may be trying to force things you don’t understand. Why don’t you describe for us what your thousand foot view of the goal is, and we can guide you with the technical aspect.

I am trying to create an object that acts like a primitive. An example would be a class USDollar instead of Currency. I know that there are other ways to accomplish all of this, but I am seeing what is possible with xojo.

Well, for starters you wouldn’t want to start with Integer, you would want to use a Double.

Obviously. It was an example, don’t get to drawn into the details of implementing currency math.

I just don’t want you to waste your time trying to debug a problem that can be caused by starting with the wrong data type.

As far as what you’re trying to do, I do not understand why you have an aversion to using the ‘New’ keyword, and for this use case, the point may be moot. Doubles default to 0.00 in Xojo.

[quote=416610:@erin jamroz]Is there a way to have a default value for a custom class similar to what xojo primitives do? Like an implicit default constructor?
For example, lets say myCustomClass is a wrapper for integer and has operator_add, _addRight and _convert(value as integer) implemented.

dim mcc as myCustomClass
dim ii as integer = 14 + mcc

Is there a way to get the code above to work without a nilobjexception and without explicitly stating that mcc is new myCustomClass = 0?

A usage would be to have these as properties of other classes that do not need to be explicitly instantiated to a default value.[/quote]

You can do some of what you asked.

Operator Convert has 2 forms
operator_convert() as TYPE - this is the “convert to” operator that is often implemented on classes
and there is operator_convert(t as type) - this is the “convert from” form.

And once you have the “convert from” form then you could write:

dim c as currency
dim u as usdollar

u = c

and the last line invokes the “convert from” form, and a new us dollar instance is created from the currency.

Some things to note though - since the operator_convert from style does NOT call the constructor IF there is a requirement that it be called then you should do that in the operator_convert. Here’s an example project:

https://blog.xojo.com/wp-content/uploads/2018/12/example.xojo_binary_project.zip

I know its off topic but I would recommend against performing any type of currency calculations using floating point. You should be using Currency or Integer (by storing cents / pennies etc…).