No announcement yet.

multiple interface types ... how to use them... sort of

  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple interface types ... how to use them... sort of

    Hi, I wasn't able to cover the problem in one subject line...hence the
    slightly mysterious description.

    Ok, this is the problem. I have a method called GetField, at the
    moment this method returns a iField interface. This is a generic
    object that contains the majority of properties and methods related
    to window controls. The method GetField is part of a class called
    cForm with an interface iForm.

    Now, I have a new control or iField as I called it. A Tab control.
    Of course the tab control is far more different than say a label or
    an Edit box control so my idea is to create a new class cTabControl
    with an interface iTabControl.

    Ok, so far so good. everybody following this ? Now, I also have
    another class called cForm which has a method called AddField(). The
    AddField method takes a parameter (ByVal ClsField as iField) this works
    for Labels, Checkboxes, Editboxes but won't work for the iTabControl

    So my first question. How can I pass object reference to a method where
    the object can be different interfaces ?

    also, How can I obtain an object reference using the method GetField(n)
    , where n is the field index number, for which the reference can be
    either iField or iTabControl. I'm not working with COM objects, just
    PB classes.

    So, in short... the iField interface is now passed as a parameter to
    AddField(ByVal ClsField as iField) but I would like to pass any kind of
    object to this method (iTabControl f.i. as well).

    Ok, maybe it's because I'm writing this that my brain starts working. I was thinking of using a UDT with two members (pObjPtr holding the ObjPtr of the object I would like to pass and bType holding the type of the object.) Than
    in stead of storing the iField in an array, I could store the UDT in an array and use a reverse method based on checking the type of object I've passed to retrieve the object using GetField. But then I use pointers instead of the
    interface directly. Did I now just answer my own question or are there better approaches to this ? Like in Java one can return an object called Object which is the mother of all objects and then with InstanceOf determine the
    object 'class' being returned. but that is Java and this is PB...

    Thanks for reading !!


    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

  • #2

    Hi Steven,

    You might want to consider looking at a Union for something like that. The way something like that is set up is that the first member of the union is some sort of integer identifier of what the other member of the union is. The other members of the union can be structures/types of any sort or size, and the compiler will create the union structure to be large enough to hold the largest of the types that it may contain. Below is an example of how a union might be used. This example pertains to some work I was doing last summer to examine a line of VB code to figure out what it is, i.e., an assignment statement, a VB Verb such as Mid(), or perhaps athe beginning of some block logic structure such as an If Or For loop. I created an iType variable as the 1st member of the union, and added three other types to at as can be seen below. I 1st saw this setup in studying the X Window System in Linux, as that is how that system handles window messages (notice I didn't say Windows!). The 1st member is an equate for the type of message, and the other members are structures (types) for each specific message.

    Type vbAssignmentStatement
      iType                        As Long
      LeftTerm                     As Term    '256
      RightTerm                    As Term    '256
      LeftTermIdentity             As Long    '  4  1=%NumericConstant, 2=%StringConstant, 3=vbObject, 4=%Integer, 5=%Long, 6=%Single, 7=%Double, etc
      RightTermIdentity            As Long    '  4  ditto
    End Type
    Type vbStatement
      iType                        As Long
      Statement                    As Term                         
      szReservedWord               As Asciiz*16             '16
      szObjectMethodCall           As Asciiz*32             '32
      szVariableDeclaration        As Asciiz*16
      szArguements                 As Asciiz*48             '48
      Identity                     As Long
    End Type
    Type vbBlockStatement
      iType                        As Long
      szStatement                  As Asciiz*32
    End Type
    Union vbCodeLine
      iType                        As Long
      vbAssignment                 As vbAssignmentStatement
      vbVerb                       As vbStatement
      vbBlock                      As vbBlockStatement
    End Union


    • #3
      Thanks Fred,

      I'll check this out... btw, looking at your code... were you trying to construct a Abstract Syntax Tree for some scripting language ?
      So here we are, this is the end.
      But all that dies, is born again.
      - From The Ashes (In This Moment)


      • #4
        Hi Stephen,

        Not sure what an 'Abstract Syntax Tree' is, so I can't answer your question. However, the union construct makes a good choice when you need some kind of container that can hold various kinds of different things. However, they can only hold one thing at a time, and that 'thing' is indicated by the integer identifier that forms the first member of the union, and also the first member of every type added to the union.

        Here just a bit ago I tried to see if interfaces could be put in a union, and it didn't seem to want to work.