Implicit constructor

  1. last week

    erin j

    Dec 3 Pre-Release Testers, Xojo Pro

    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.

  2. Jonathan E

    Dec 3 Pre-Release Testers, Xojo Pro Las Vegas, NV

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

  3. erin j

    Dec 3 Pre-Release Testers, Xojo Pro

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

  4. Jonathan E

    Dec 3 Pre-Release Testers, Xojo Pro Las Vegas, NV

    Using that method, yes.

    Dim mcc as New myCustomClass
  5. Tim P

    Dec 3 Pre-Release Testers feedback://46303
    Edited last week

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

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

    @erin j dim mcc as myCustomClass
    dim ii as integer = 14 + mcc

    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.

  6. erin j

    Dec 3 Pre-Release Testers, Xojo Pro

    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.

  7. Jonathan E

    Dec 3 Pre-Release Testers, Xojo Pro Las Vegas, NV

    @erin j 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.

  8. erin j

    Dec 3 Pre-Release Testers, Xojo Pro
    Edited last week

    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.

  9. Jonathan E

    Dec 3 Pre-Release Testers, Xojo Pro Las Vegas, NV

    @erin jamroz 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.

  10. Jason P

    Dec 4 Xojo Inc Texas
    Edited last week

    @erin j 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.

    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

  11. Kevin G

    Dec 4 Pre-Release Testers, Xojo Pro Gatesheed, England

    @Jonathan E Well, for starters you wouldn't want to start with Integer, you would want to use a Double.

    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...).

or Sign Up to reply!