[Gambas-user] Direct assignment to arrays of inherited classes does not work
Tobias Boege
taboege at ...626...
Mon Sep 22 10:46:49 CEST 2014
On Mon, 22 Sep 2014, Julio Sanchez wrote:
> Direct assignment to arrays of inherited classes does not work
>
> We have a class that inherits from the integer class [], we call
> SuperInteger, this is your code:
> Inherits Integer []
>
> Public function higher() as integer
> dim itemt as integer
> dim ihigher as integer
>
> for each element in me
> ihigher = max (ihigher, item)
> next
>
> return ihigher
>
> end
>
>
> When I make an instance of the new :
>
> dim test as new SuperInteger
>
>
> It does not work this assignment:
> test = [86, 16, 212, 1, 43, 18]
>
> gambas3 says it's not the same type.
> Should I working? Right?
>
No, because it is not the same type :-)
Even if B inherits A, it can be very difficult to make sense of an
assignment myB = myA because myB could reference _lots_ of other
information (which is not in the class A). What should happen to this
information in the assignment? You simply can't convert myA to the B
class.
The clean solution would be to define a static B.FromA() method which
takes an A object and returns a new B object which contains that A
object and initialises all other information appropriately.
So, you would implement
Static Public Function FromArray(aArray As Integer[]) As SuperInteger
or something along these lines in SuperInteger. For a more concise
syntax, you may also use the _call() special method. Then you can do
myB = SuperInteger([1, 2, 3, 4])
Regards,
Tobi
--
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
More information about the User
mailing list