Page 1 of 1

Explicit type conversion

Posted: Monday 14th February 2022 6:42pm
by Witchi
Hi,

Is it possible to use a generalized event which transports different parameter types?
I have a parent class, which defines such an event, but the sub classes implement the event creation in different ways.
' on class AbstractLoader
Event finish(obj as Variant)
Now I try to raise such an event on different parts of the code

i.e.
' on class LoaderA inherits AbstractLoader
Raise finish(new MyTestA()) 
or
' on class LoaderB inherits AbstractLoader
Raise finish(new MyTestB())
The classes MyTestA and MyTestB don't have the same parent class, so I create complete different objects within the subclasses.
The classes which collect the events, should check the type of the transported object, but must convert them into the right type:

Dim loader as LoaderA
loader = new LoaderA as "myloader"

public sub myloader_finish(obj as Variant)

' here I have to convert the "obj" into "MyTestA" again, to use the
' right type later

end
With other programming languages I would use explicit type conversion like
Dim my as MyTestA
my = (MyTestA) obj
But this seems not possible in Gambas3. How I could do that?

Thanks
Witchi

Re: Explicit type conversion

Posted: Tuesday 15th February 2022 6:27am
by AMUR-WOLF
Public Sub myloader_finish(obj as Object) ' Object is better than Variant 
 
  ' here I have to convert the "obj" into "MyTestA" again, to use the
  ' right type later
  If obj Is MyTestA Then
    Dim my As MyTestA = obj
    my.SubFromMyTestA()
  Endif 

End

Re: Explicit type conversion

Posted: Tuesday 15th February 2022 8:53am
by AMUR-WOLF
I created a complete example project for you:
Loader.tar.gz
(12.41 KiB) Downloaded 168 times

Re: Explicit type conversion

Posted: Tuesday 15th February 2022 10:27am
by BruceSteers
Yes Object is the best type to use as it can be anything.

You can use Object.Propertyname for any object type but the property name must exists.

Object.Type() can be used to determine the variable type

Here is a Function i made for when loading my apps settings.

I use it to set the values of my forms controls but it uses Object.Lock before setting the value so the control does not trigger events.

I call it like this..
SetValue(cbxCheckBox, Settings["CheckValue", False])
SetValue(dbDirBox, Settings["DirText", ""])
SetValue(cmbMyComboBox, Settings["ComboIndex", 0])
I then use Object.Type() to find what the object is so i know if the property to set needs to be .Text , .Value or .Index etc

Public Sub SetValue(obj As Object, Value As Variant, Optional SetText As Boolean)

  Dim sType As String

  If SetText Then sType = "t"  ' force .Text type and do not even check

  If Not sType Then
    Select Object.Type(obj)
      Case "SliderBox", "CheckBox", "ToggleButton"
        sType = "v"
      Case "TextBox", "TextArea"
        sType = "t"
      Case "ComboBox", "ListBox"
        sType = "i"
    End Select
  Endif

  Object.Lock(obj)

  Select sType
    Case "t"
      obj.Text = Value
    Case "i"
      obj.Index = Value
    Case Else
      obj.Value = Value
  End Select
  Object.UnLock(obj)

End


Also check out Param.class and using the 3 dots '...'
http://gambaswiki.org/wiki/comp/gb/param

Re: Explicit type conversion

Posted: Tuesday 15th February 2022 10:34am
by BruceSteers
PS.
You can also assign a variable pointer of the required type to an Object

Ie..

Public Sub ObjectExample(obj As Object)

 Select Object.Type(obj)
    Case "Window"
     Dim w As Window = obj  ' make a Window variable and assign obj to it

     w.Resize(400,400)  ' Now w. has the window properties/methods in the form designer

  ' here obj.Resize() will also work but it will not pop auto-completion for you in the IDE
  End Select

End

Think of Variant for things like Strings, Integers, Booleans etc but for controls and classes use Object

Re: Explicit type conversion

Posted: Tuesday 15th February 2022 10:42am
by BruceSteers
I may have over-complicated that and thought maybe this is all you need...
Dim my As AbstractLoader 
my = MyTestA()
PS. that coding style implies MyTestA is a Function that returns an instance of AbstractLoader (so it MUST end in parenthesis() if it returns something)
Public Sub MyTestA() As AbstractLoader
  Return $myAbstractLoaderVariable
End

Re: Explicit type conversion

Posted: Tuesday 15th February 2022 10:51am
by AMUR-WOLF
BruceSteers wrote: Tuesday 15th February 2022 10:27am Object.Type() can be used to determine the variable type
Object.Type() returns the exact class name as a string, so it cannot be applied for polymorphic type determine.
  Dim obj As New ExcellentObserver(Me) 
  ' Class 'ExcellentObserver' inherits class 'Observer'
  ' Let's check if obj has type 'Observer'
  Print Object.Type(obj) = "Observer"
  ' Let's do the same but polymorphic way
  Print obj Is Observer
Result:
False
True

Re: Explicit type conversion

Posted: Thursday 24th February 2022 1:22pm
by Witchi
Thanks for all posts. It works with Object instead of Variant! :)

Re: Explicit type conversion

Posted: Saturday 26th February 2022 8:48am
by thatbruce
AMUR-WOLF wrote: Tuesday 15th February 2022 10:51am
BruceSteers wrote: Tuesday 15th February 2022 10:27am Object.Type() can be used to determine the variable type
He meant to say "the instance type"