Announcement

Collapse
No announcement yet.

How to evaluate for interfaces ?

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

  • How to evaluate for interfaces ?

    Hi,

    The following will illustrate what I'm after...

    assume one has a class (methods and properties are left out for easier understanding)

    Code:
     class cField
    
    	interface iField
    	inherit Dual
    	end interface	
    
     end class
    and another class inheriting the ifield interface

    Code:
     class cCheckbox
    
         interface iCheckbox
         inherit cField, iField
         end interface
     end class
    There would be a bunch of other classes that mimic the usage of controls
    in a window. but for illustration purposes the checkbox will suffice.

    Now, I have a form class with a method AddField and GetField which
    would look like this

    Code:
     class cForm
         interface iForm
         inherit Dual
    
         method Addfield(ByVal clsField as Dispatch)
    
    	... collection handling is here... 
    	actually just an dispatch array holding the 
            clsfield object
    
         end method 
    
         method GetField(byVal nId as long) as Dispatch
    
    	... lookup field here by evaluating the id which
            ... is a property of the iField interface.
    
            myDispatch = m_FieldCol(n)
    
            method = myDispatch
    
         end method 
    
    
         end method
    
     end Class
    Now, I wish I could evaluate for certain interfaces.

    Code:
      Local clsField		as Dispatch
    
      clsField = MyForm.GetField(Id)
    
      select case <INTERFACE QUERY ON OBJECT HERE>
      case "IFIELD" ' Base class
    \ Case "ICHECKBOX" ' A checkbox... what can we do with
    				   a checkbox that we
    				   can't do with the baseclass.
    
           ' We need to use object Call here since we're referencing
           ' a dispatch interface instead of the direct interface.
    
           Object Call clsField.SetTrue()
    
      End Select
    So, all this stuff just to ask, can we get the interface name of an
    object by some kind of trick ?

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

  • #2
    Ok, thinking about this I could set a property that
    would indicate the type of interface I'm dealing with
    using an instance variable.

    but I was hoping for a more elegant solution.
    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

    Comment


    • #3
      >..could set a property that would indicate the type of interface I'm dealing with....
      >but I was hoping for a more elegant solution

      Some of us find simplicity elegant.
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        I think what you are looking for is named RTTI (Run-Time Type Information) in OOP mumbo-jumbo. One way of getting it is to set up an enumeration a variable of which type can be an instance variable in your ultimate base class. When you create each object of various derived classes set a simple enum constant to some value that can later be tested.

        http://en.wikipedia.org/wiki/RTTI
        Last edited by Fred Harris; 4 Mar 2009, 09:45 AM.
        Fred
        "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

        Comment


        • #5
          Hi Fred,

          Yes, kind of however instance variables are not accessible from inherited classes so I would have to use a get/set property.
          So here we are, this is the end.
          But all that dies, is born again.
          - From The Ashes (In This Moment)

          Comment


          • #6
            Oh, and BTW, ....

            I wish one could do

            Object.method or Object.property with dispatch interfaces too... really...
            it bothers me a bit I have to do

            Code:
              Object Get Object.Property to hResult
            whereas it would be soooo easy to have

            If object.property = ... then

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

            Comment


            • #7
              Steven,

              did you have a look at ISINTERFACE?
              It allows to test whether object has specified interface.

              So you can do:
              Code:
              IF ISINTERFACE(obj, ICheckBox) THEN
              ELSEIF ISINTERFACE(obj, ILabel) THEN
              ...
              END IF

              Petr
              [email protected]

              Comment


              • #8
                Hi Peter,

                Sure, I checked it out but that doesn't do much good. I need to have the name of the interface. However, the use of a property solves the issue quite nicely.

                Thanks for looking into this !

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

                Comment

                Working...
                X