Results 1 to 7 of 7

Thread: Interface dll in ocx

  1. #1

    Thread Starter
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183

    Interface dll in ocx

    Hi

    I have an interface that I have put in a separate dll.
    I implement this interface in a user control ocx.

    In my main program, i have the dll and ocx referenced,
    and I load the user control depending on which form is showing.

    I use this code to do it:

    VB Code:
    1. Private Sub Form_Load()
    2.    Dim oCtl As Object
    3.    Dim ctlMain As FormControls.ctlBuy
    4.    Dim clsMain As FormClasses.clsForm
    5.  
    6.    Set oCtl = Controls.Add("FormControls.ctlBuy","ctlCur")
    7.    Set ctlMain = oCtl
    8.    ' I set the control's properties and show it here...
    9.    Set clsMain = ctlMain
    10. End Sub

    This works fine until the statement 'Set clsMain = ctlMain'.
    This gives a type mismatch error.

    The dll is compiled with binary compability, and was compiled
    before I started making my user control. I have checked that
    it is the same dll that is referenced both from the main app and
    from the ocx project.

    What am I doing wrong here?

  2. #2
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    uk
    Posts
    327
    ctlMain and clsMain must implement the same interface. Do they?

  3. #3

    Thread Starter
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183

    Interface

    The FormClasses.dll contains just one class module,
    which is clsForm. clsForm is implemented in FormControls.ctlBuy.

    I then try to define an object of type clsForm, and set this to
    be an instance of FormControls.ctlBuy.

    It worked fine as long as i had all the class modules, user
    controls, and forms in the same project, but when i split the
    project into 3 parts, 1 ocx, 1 dll, 1 exe, i got the type mismatch
    error message.

    So clsMain is the interface class, and ctlMain implements that class.

  4. #4
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    uk
    Posts
    327
    Have you tried taking out the intermediary - you don't need both oCtl and ctlMain.
    Code:
    Set ctlMain = Controls.Add("FormControls.ctlBuy","ctlCur")
       ' I set the control's properties and show it here...
       Set clsMain = ctlMain
    If this still gives a type mismatch error, then there's something wrong with your interfaces or controls.

    Just a tip - give things more obvious names. The coding convention for interface classes is to prefix with "I" - FormClass.IForm for example. Classes which implement this interface are prefixed with "C" to stand for Concrete. Objects are prefixed with obj or nothing at all... so (Just IMHO of course):
    Code:
    Dim objCtl As Object
       Dim objBuy As FormControls.CBuy
       Dim objForm As FormClasses.IForm

  5. #5

    Thread Starter
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183

    Test project

    OK.. I did a simple test project, and got the same problem.
    Here is what I have:

    FormClasses.IForm:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Property Let ControlCaption(ByVal sCaption As String)
    4.  
    5. End Property
    6.  
    7. Public Property Get ControlCaption() As String
    8.  
    9. End Property

    FormControls.ctlLabel:
    VB Code:
    1. Option Explicit
    2.  
    3. Implements FormClasses.IForm
    4.  
    5. Private Property Get IForm_ControlCaption() As String
    6.    IForm_ControlCaption = Label1.Caption
    7. End Property
    8.  
    9. Private Property Let IForm_ControlCaption(ByVal RHS As String)
    10.    Label1.Caption = RHS
    11. End Property

    prjTest:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdTest_Click()
    4.    Dim ctl As FormControls.ctlLabel
    5.    Dim cls As FormClasses.IForm
    6.    Set ctl = Me.Controls.Add("FormControls.ctlLabel", "lblNew")
    7.    ctl.Top = cmdTest.Top + cmdTest.Height
    8.    ctl.Left = cmdTest.Left
    9.    ctl.Visible = True
    10.    Set cls = ctl
    11.    cls.ControlCaption = "Hello"
    12.    Set cls = Nothing
    13.    Set ctl = Nothing
    14. End Sub

    I still get the Type Mismatch error in the line "Set cls = ctl"
    I noticed that VB changes the sCaption parameter to RHS,
    but changing it back to sCaption in the usercontrol doesn't
    make any difference.

  6. #6
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    uk
    Posts
    327
    In that case I can't see anything wrong. It all looks fine... perhaps it's something to do with the Controls.Add method... try using a control array instead, and using the Load statement to create a new instance.

  7. #7

    Thread Starter
    Addicted Member Bregalad's Avatar
    Join Date
    Jul 2000
    Location
    Oslo,Norway
    Posts
    183

    Solved (?)

    I just changed the line
    VB Code:
    1. Set cls = ctl

    to

    VB Code:
    1. Set cls = ctl.Object

    This seemed to do the trick,
    although I have no idea why it should.

    Thanks for your help ChiefRB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width