[Gambas-user] Direct assignment to arrays of inherited classes does not work

Julio Sanchez jusabejusabe at ...626...
Mon Sep 22 11:03:49 CEST 2014


Thanks Tobi, I'll try :)

Regards,

Julio

2014-09-22 10:46 GMT+02:00 Tobias Boege <taboege at ...626...>:

> 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
>
>
> ------------------------------------------------------------------------------
> Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
> Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
> Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
> Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
>
> http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>



More information about the User mailing list