[Gambas-user] About class and components

Tobias Boege tobs at taboege.de
Tue Feb 2 12:50:13 CET 2021


On Tue, 02 Feb 2021, Olivier Coquet wrote:
> Auto-Reply.
> 
> First:
> 
> Public cmp_comp As Component = Component.Load("component name")
> 
> Second:
> 
> CLASS *Classe_in_component*
> 
> third:
> 
> Public MyClass as *Classe_in_component*
> 
> "Et voila".
> 

There is another way using introspection. Once you loaded the component,
its classes will be registered in the global symbol table (of classes).
You can also obtain Class objects (not the Class syntax keyword that you
used before) from the Classes class:

  ' Will find the class once its component is loaded
  Dim hClass As Class = Classes["Classe_in_component"]

You cannot use this to declare variables using Public or Dim but those
need a compile-time class name, but you can use this Class object to
create new objects of that class nevertheless, at runtime:

  Dim hObj As Object = hClass.New([ any arguments go here ])

Just type your new object as an Object. Gambas will still resolve method
calls and property accesses dynamically, even if hObj does not have the
static type Classe_in_component.

See the wiki [1] for what else you can do with a Class object.

For plugins, what is better depends at least on whether:

  - there is a specific singular functionality that you put into a
    plugin because it is optional and would introduce a dependency
    that you don't want to put on everybody (e.g. your coffee machine
    gets an email notification plugin which requires the expensive
    filters with SMTP support), or

  - you have multiple plugins for the same functionality (e.g.
    audio codecs) that expose the same interface but where many
    can be loaded concurrently.

In the first case, all the class names should be fixed and I think it
would be more convenient to work with your first solution of forward-
declaring the class at compile time.

In the latter case, I would use introspection and keep interface Class
objects around in a Collection of (audio) plugins over which you can
iterate and ask each plugin if it knows what to do with a given file.

Best,
Tobias

[1] http://gambaswiki.org/wiki/comp/gb/class

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


More information about the User mailing list