[Gambas-user] About class and components

Tobias Boege tobs at taboege.de
Tue Feb 2 20:41:52 CET 2021


On Tue, 02 Feb 2021, Olivier Coquet wrote:
> Thank's Tobias, I've read all :)
> 
> A misunderstood for me, why statics var are not accessible after instancing
> the class ?
> 

Not static variables but static typing of variables. You cannot just write

  Component.Load("thecomponent") ' loads myclass
  Dim x As myclass ' error: "myclass" not found

or

  Component.Load("thecomponent")
  Dim c As Class = Classes["myclass"]
  Dim x As c ' error: still not found, as a c is just some runtime object
             ' but variable declaration wants to know "c" at compile time

because the right-hand side of the "As" in a variable declaration must be
a word known at compile time as a class. You can do this as you first did
by forward-declaring the word as a class that "will be supplied later".

My point was that you do not need to do this at all. You can instantiate
a dynamically loaded class into a variable with a generic Object or Variant
type. You don't need the static type:

  Component.Load("thecomponent")
  Dim c As Class = Classes["myclass"]
  Dim x As Object = c.New() ' OK

Above, even though the Object class has no methods, if you call methods
of the myclass class on x, it will work, because Gambas looks at the
actual type of an object to resolve methods, not at the type of a variable
holding the object.

Best,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list