[Gambas-user] Rendering an object as one of its classifier's subtypes.

Bruce bbruen at ...2308...
Sun Jul 29 04:02:20 CEST 2012


I have a situation where objects created later need to be
"rendered" (cast?, reclassifed?) as one of many specialised
versions of those objects.  In the following code all went fine until I
added the check to ensure that the specialised class is a descendant of
the original class.

Now it fails when compiling at the marked line with an "Unexpected
Object" object error.
Can anyone tell me what I'm doing wrong here?

-----code----
        Public Sub Main()
          Dim obj1 As New Parent
          Dim obj2 As Child1
          Dim c1 As New Collection
          
          With obj1
            .Name = "c1"
            .Type = "1"
          End With
          c1.Add(obj1, obj1.Name)
          
          obj1 = Morph(obj1, "child1")
          c1[obj1.Name] = obj1
        
          Print obj1 Is Parent
          Print obj1 Is Child1
          Print obj1 Is Child2 '(a different subtype of "parent")
        
          Print Object.Class(obj1).Count
          
        End
        
        Private Function Morph(source As Object, targetname As String)
        As Object
        
          Dim nt As Object
          Dim propname As String
          
          nt = Object.New(targetname)
          
          If Not(nt Is Object.Class(source)) Then          ' <<--- HERE!
            Error.Raise("Illegal morph")
          Else
            For Each sprop In Object.Class(source)
              If Object.Class(source)[sProp].Kind = Class.Property Then
                Object.SetProperty(nt, sProp, Object.GetProperty(source,
        sProp))
              Endif
            Next
          Endif
          
          Return nt
        
        End
--- end ----

tia
Bruce





More information about the User mailing list