Announcement

Collapse
No announcement yet.

Dynamic Class Creation?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic Class Creation?

    So I am thinking through the first project that I would like to use objects for and hit an immediate road block that I am hoping there is a quick answer to. I have a need to be able to dynamically create classes. So let's say I have a class called Class1 and it implements two interfaces called Interface1 and Interface2. I would like to be able to create 10 instances of the object at runtime each using an interface that I detect at runtime. So my question centers around the following code:

    Local oInterface1 As Interface1
    Let oInterface1 = Class "Class1"

    What if I don't know at compile time whether I will need Interface1 or Interface2? Any ideas how to safely do this or even how to do this at all? The ideal solution would be something like a function I could call that would return an instance of the proper Interface.

    Anyone have any ideas or is this simply not possible?
    Mac Application Reviews and More

  • #2
    Ken,
    There may be a couple of different ways depending on how different your class/interfaces are. In this example in the source code forum:
    http://www.powerbasic.com/support/pb...ad.php?t=38252
    I was a virtual base class and some magic.

    Remember also one interface can begat another ( that's a Sunday word isn't it)

    If you have more than one interface in your class: oIface2 = oInface1

    James

    Comment


    • #3
      Not Working

      I tried to extend what you provided, but I just don't think this is doing what I want it to do. I also tried faking it a number of ways... I think what you are doing will only work if the objects share a common interface...

      Code:
      Sub CreateOne(ByRef MyobjPtr As Dword)
         Dim oMyObj As Interface1
         Let oMyObj = Class "Class1"   
         Poke Dword,VarPtr(MyobjPtr),ObjPtr(oMyObj) 
         If IsNothing(oMyObj) Then 
          MsgBox "not an object",16,"test2"
          Exit Sub
         Else
          MsgBox "it's an object",16,"test3"
         End If         
      End Sub
      Then a snip from the test code...

      Code:
          Local iObj As Interface1
          Local myDword As Dword
          CreateOne(ByVal VarPtr(iObj))
          If IsObject(iObj) Then MsgBox "iObj is an object!",16,"test6"       
          VD_StdOut iObj.MyClassMethod(5)
      This works, but not if I try to use Interface2...

      I'm not sure that what I want to do is possible. Mainly because unless I specify the class for each interface then the methods are not accessible (the compiler complains at compile time).

      -Ken
      Mac Application Reviews and More

      Comment


      • #4
        Reflection

        I think what I am looking for is something like what I have seen in other OOP languages like java and c#. A possible solution might be reflection. Is there a PB equivalent?
        Mac Application Reviews and More

        Comment


        • #5
          Can not entirly follow but may using it as a dual interface?
          And then late bound calls?
          hellobasic

          Comment

          Working...
          X