Explicit type conversion

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
Witchi
Posts: 7
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Explicit type conversion

Post by Witchi » Monday 14th February 2022 6:42pm

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

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 06th February 2022 8:41am
Location: Russia

Re: Explicit type conversion

Post by AMUR-WOLF » Tuesday 15th February 2022 6:27am

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

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 06th February 2022 8:41am
Location: Russia

Re: Explicit type conversion

Post by AMUR-WOLF » Tuesday 15th February 2022 8:53am

I created a complete example project for you:
Loader.tar.gz
(12.41 KiB) Downloaded 48 times

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Explicit type conversion

Post by BruceSteers » Tuesday 15th February 2022 10:27am

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

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Explicit type conversion

Post by BruceSteers » Tuesday 15th February 2022 10:34am

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

BruceSteers
Posts: 766
Joined: Thursday 23rd July 2020 5:20pm

Re: Explicit type conversion

Post by BruceSteers » Tuesday 15th February 2022 10:42am

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

User avatar
AMUR-WOLF
Posts: 21
Joined: Sunday 06th February 2022 8:41am
Location: Russia

Re: Explicit type conversion

Post by AMUR-WOLF » Tuesday 15th February 2022 10:51am

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

Witchi
Posts: 7
Joined: Monday 14th February 2022 6:27pm
Location: Germany

Re: Explicit type conversion

Post by Witchi » Thursday 24th February 2022 1:22pm

Thanks for all posts. It works with Object instead of Variant! :)

User avatar
thatbruce
Posts: 26
Joined: Saturday 04th September 2021 11:29pm

Re: Explicit type conversion

Post by thatbruce » Saturday 26th February 2022 8:48am

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"

Post Reply