Dim variableName as object

On other programming languages, you can set a variable on general object type (like Dim variable as object). Then you can assign any type of other object on that (like class, etc). I need it, because I want to return a class property, of any type of variable.

Can you propose a workaround ?

BR/Antonis

You can use a Variant for the data type.

You can do that in Xojo. dim o as object works fine, but you then have to make sure you know what type of object you’re working with. You can use the isa syntax to see what type of object it is.

Another option is the variant type, as in dim v as variant which can be defined as any kind of data type.

Both of these methods can get complicated, however, since they can contain various kinds of objects and values. You may need to tell the compiler what type of object you’re working with, like this:

if v isa myClass then return myClass(v)

This looks weird, but tells the compiler that the property v is of type myClass and to return a myClass object. This will crash if v isn’t of type myClass, but that won’t happen in the above code because we’ve tested it with the isa command.

[quote=22376:@Antonis Vakondios]On other programming languages, you can set a variable on general object type (like Dim variable as object). Then you can assign any type of other object on that (like class, etc). I need it, because I want to return a class property, of any type of variable.
Can you propose a workaround ?
[/quote]
What other languages are you referring to (I’m curious to know what sort of back ground you have)

What are you doing that you cant declare the correct type ?
Use Variants and Object sparingly
Being strictly typed should work in your favor

What is it you’re doing that you want to return generic objects ?

Hi Norman, I have good experience from javascript node.js world.

For example, I want to implement the following function as Xojo class and the resultObject to be any generic object (ORM class, webclass, etc)

BL.Utils.Returns = function(Result, ErrorCode, ErrorDescription, ResultObject)
{
this.result = Result;
this.errorCode = ErrorCode;
this.errorDescription = ErrorDescription;
this.resultObject = ResultObject;

};

Then, on every Method/function, I can return for example:
return new BusinessLogic.Utils.Returns(true,0,‘Database initialized’,DBConnectObject);

INCLUDING the processed Object

BR/Antonis

Hi Norman, I have good experience from javascript node.js world.

For example, I want to implement the following function as Xojo class and the resultObject to be any generic object (ORM class, webclass, etc)

BL.Utils.Returns = function(Result, ErrorCode, ErrorDescription, ResultObject)
{
this.result = Result;
this.errorCode = ErrorCode;
this.errorDescription = ErrorDescription;
this.resultObject = ResultObject;

};

Then, on every Method/function, I can return for example:
return new BL.Utils.Returns(true,0,‘Database initialized’,DBConnectObject);

INCLUDING the processed Object

BR/Antonis

  1. dont write javascript in Xojo - you’ll hate it. Xojo IS strongly typed (more like java)

  2. I’d consider making “result object” an interface - then anything that implements that interface can be a “ResultObject” since interfaces ARE types and Xojo can & will do type checking for you

Consider writing Xojo code more like Java (not js) where things are strongly typed and not loosely typed like js typically is used.
Doing so the compiler can help you avoid errors & catch them at compile time otherwise you have to try & debug it with no type safety at run time and that’s much harder.

Good solution,

Thanks