OO programming question

Hi,

I have the following problem. Let’s assume you have a class TObject which is Super of some classes as TDouble, TInteger, etc…
I want to implement a virtual method Value() in TObject to be overwritten in the derived classes. The problem is that in TDouble
I want to return a double, in TInteger I want to return an integer, etc…

Is there a way to achieve this?

Thanks.

use a variant ?

[code]Class TObject

Private mValue As Auto

Protected Sub Constructor() // Prevent creation of a TObject instance
Raise New RuntimeException()
End

Protected Sub Constructor(value As Auto) // Prevent creation of a TObject instance
mValue = value
End

Function Value() As Auto
Return mValue
End

Function Operator_Convert() As Auto // Return mValue without using the Value method
Return mValue
End

End Class

Class TDouble Inherits TObject

Sub Constructor(value As Double)
Super.Constructor(value)
End

End Class

Class TInteger Inherits TObject

Sub Constructor(value As Integer)
Super.Constructor(value)
End

End Class[/code]

[code]’ Dim o As New TObject() // will not compile
’ Dim o As New TObject(123) // will not compile
’ Dim o As New TObject(456.789) // will not compile

Dim i As New TInteger(123)
Dim d As New TDouble(456.789)

Dim r1 As Integer = i.Value
Dim r2 As Integer = i[/code]

Use OVERLOADS
the object can have multiple constructors as long as the signatures are different

You cannot overload a method which returns a value.
You would need two different parameter lists to be able to overload.

uh… yeah you can

function test(x as integer) as integer
function test(x as double) as integer
function test(x as string) as integer

all quite valid… what you cannot do is alter the type of the RETURN value
and methods obviously have no such restriction

[quote=329459:@Eli Ott]You cannot overload a method which returns a value.
You would need two different parameter lists to be able to overload.[/quote]
I think what you mean is you cannot overload SOLEY on return type
This would not work

function test(x as integer) as integer
function test(x as integer) as double
function test(x as integer) as string

a valid overload requires the values between the ( ) to be different datatypes (and can be a different number of arguments), if there is a return value they must all be the same… with one exception… when you are emulateing a computed property. A normal CP cannot accept arguments, but it is possible to create a function and method with the same name to achieve the same result

FUNCTION test(x as integer,y as integer) as integer // this would be the equivalent of GET
SUB test(x as integer,y as integer,assigns newvalue as integer) // this would be the same as SET

thank you guys for all the inputs. @Eli Ott Thank you for the code, I will take a look at it.
I was thinking that another approach could be to make mValue an instance of TObject itself.
In that case for example TDouble would return TDouble (which is possible since derived from TObject).
Does it make sense to you?

Combine this with the Operator_Lookup and you can get around the overloading issue. Not an obvious solution, but does work.

Remember that Operator_ methods don’t AutoComplete.

You could have it return auto so you get exceptions if the type does not match on assignments.