Autore Topic: L'assegnazione diretta a vettori di classi ereditate non funziona  (Letto 495 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
Vi riporto questa discussione:


" We have a class that inherits from the integer class [], we call
SuperInteger, this is your code:

Codice: gambas [Seleziona]
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 :
Codice: gambas [Seleziona]
dim test as new SuperInteger

It does not work this assignment:
Codice: gambas [Seleziona]
test = [86, 16, 212, 1, 43, 18]

gambas3 says it's not the same type.
Should I working? Right?

Regards

Julio
"


" 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

Codice: gambas [Seleziona]
 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

Codice: gambas [Seleziona]
 myB = SuperInteger([1, 2, 3, 4])


Regards,
Tobi
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.723
  • Ne mors quidem nos iunget
    • Mostra profilo
Re: L'assegnazione diretta a vettori di classi ereditate non funziona
« Risposta #1 il: 22 Settembre 2014, 20:38:01 »
...continua...


" strange...

has we can say Control = TextBox ... no ?

for me if B inherit A so it can contain a A class ... it have the same
base struct...


For example in gb.report a reportcontrol can contain a ref to a
reportLabel witch inherit  reportcontrol... why it is not the same for
arrays ?

Fabien
"


" No, it's the other way.

TextBox inherits Control, so you can assing a TextBox to a Control
variable, but not the contrary.

[...] returns a native array, so you can't assign it to a variable whose
datatype is a class that inherits that native array.

We need the ability to implement an automatic conversion functions
directly in Gambas, not just in C/C++. I won't do that now!

--
Benoît Minisini
"


" Yes, this is because it is the other way around. TextBox inherits Control,
so TextBox contains more information. To assign Control = TextBox, we just
need to forget some of the additional stuff in TextBox, which is easy.

Tobia
"
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »