Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 35

Thread: why cant i see the message box on screen?

  1. #1
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location

    why cant i see the message box on screen?

    Hi,
    I have a simple code made of class interface, class and module to test it:

    in my ClsInterface:
    [vba]
    Public Sub shape()
    End Sub
    [/vba]

    class2:
    [vba]

    Implements ClsInterface
    Private Sub ClsInterface_shape()
    MsgBox ("test run")
    End Sub
    [/vba]

    code to test it:
    [vba]
    Public Sub test()
    Dim obj As New ClsInterface
    obj.shape
    End Sub
    [/vba]

    why cant i see the message box?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Without testing it, I think that obj.shape call the empty Public Sub shape()

    Put your Msgbox in THAT sub and see if it works for you

    Also, I think that 'Impliments' is a VB keyword, not a VBA keyword.

    Paul

  3. #3
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by Paul_Hossler
    Without testing it, I think that obj.shape call the empty Public Sub shape()

    Put your Msgbox in THAT sub and see if it works for you

    Also, I think that 'Impliments' is a VB keyword, not a VBA keyword.

    Paul
    The class interface is abstract, all the coding is in the class that implements it.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    The class interface is abstract, all the coding is in the class that implements it.
    Yes. I never used it; I just take the default interface from a class. Learned something

    Make ClsInterface_shape Public and see if it works: From online Help

    When a Visual Basic class implements an interface, the Visual Basic class provides its own versions of all the Public procedures specified in the type library of the Interface. In
    Paul

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Paul, I've been curious about the use of Impliments for a while.

    Do you have a link to the file your quote came from?

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by mikerickson
    Do you have a link to the file your quote came from?
    That was from the Excel 2010 online help.

    I had never used / heard of 'Implements' until now.

    I rarely use Classes in VBA, and when I do they're pretty basic

    Paul
    Attached Files Attached Files

  7. #7
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by Paul_Hossler
    Yes. I never used it; I just take the default interface from a class. Learned something

    Make ClsInterface_shape Public and see if it works: From online Help



    Paul
    still doesnt work.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Module 1

    [vba]
    Public Sub test()
    Dim obj As New clsInterface
    obj.shape
    End Sub
    [/vba]


    clsInterface
    [vba]
    Public Sub shape()
    MsgBox ("test run")
    End Sub
    [/vba]

    class2
    [vba]
    Implements clsInterface
    Public Sub ClsInterface_shape()

    End Sub
    [/vba]

    Been a learning experience, but unless I'm misunderstanding something (VERY possilble), it seems to work for me

    Paul
    Attached Files Attached Files

  9. #9
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Thanks for the file.

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Thanks for the file.
    Don't say that until you're sure it works

    Paul

  11. #11
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by Paul_Hossler
    Module 1

    [vba]
    Public Sub test()
    Dim obj As New clsInterface
    obj.shape
    End Sub
    [/vba]


    clsInterface
    [vba]
    Public Sub shape()
    MsgBox ("test run")
    End Sub
    [/vba]

    class2
    [vba]
    Implements clsInterface
    Public Sub ClsInterface_shape()

    End Sub
    [/vba]

    Been a learning experience, but unless I'm misunderstanding something (VERY possilble), it seems to work for me

    Paul
    Class2 is unnecessary now, and the code treats the interface like a normal class.
    plus the class interface should be abstract, no code, all the coding is in the classes that implements the interface. instead of only 1 class you can add many more that everyone share the interface propeties/methods but still
    each one of the classes can behave differently(polymorphism).

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Yes, that part I can follow, but I can't even get the Implements on-line help example to work.


    But it's a rainy Saturday here, and there's nothing interesting on TV, so this is more fun

    Paul

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I think that defining an abstract interface just allow objects that Impliments that interface to be assigned to variables of that

    You do sort of get polymorphism, but not perfectly

    clsInterface:
    [vba]
    Option Explicit
    Public Sub shape()
    '
    End Sub
    [/vba]


    Class2:
    [vba]
    Implements clsInterface
    Public Sub ClsInterface_shape()
    MsgBox ("test run from Class2")
    End Sub
    [/vba]

    and a Class3 that Implements the same interface as Class3 but has different processing in the shape()

    [vba]
    Implements clsInterface
    Public Sub ClsInterface_shape()
    MsgBox ("test run from Class3")
    End Sub
    [/vba]

    The main sub
    [vba]

    Public Sub test()
    Dim obj As New clsInterface

    Dim obj2 As New Class2
    Dim obj3 As New Class3

    Set obj = obj2
    obj.shape

    Set obj = obj3
    obj.shape
    End Sub
    [/vba]

    It would seem to me (and you pointed it out) that in this example that you really don't need the complexity. The examples I saw used a library and books and a Collection. That would seem to me where polymorphism would be helpful. So thanks for an interesting Saturday afternoon. Much more fun than painting the bedroom.

    If you're interested, I attached the library example that I was playing with.

    I'm open to any and all suggestions

    Paul
    Attached Files Attached Files

  14. #14
    VBAX Regular
    Joined
    Aug 2011
    Posts
    18
    Location
    Quote Originally Posted by Paul_Hossler
    I think that defining an abstract interface just allow objects that Impliments that interface to be assigned to variables of that

    You do sort of get polymorphism, but not perfectly

    clsInterface:
    [vba]
    Option Explicit
    Public Sub shape()
    '
    End Sub
    [/vba]


    Class2:
    [vba]
    Implements clsInterface
    Public Sub ClsInterface_shape()
    MsgBox ("test run from Class2")
    End Sub
    [/vba]

    and a Class3 that Implements the same interface as Class3 but has different processing in the shape()

    [vba]
    Implements clsInterface
    Public Sub ClsInterface_shape()
    MsgBox ("test run from Class3")
    End Sub
    [/vba]

    The main sub
    [vba]

    Public Sub test()
    Dim obj As New clsInterface

    Dim obj2 As New Class2
    Dim obj3 As New Class3

    Set obj = obj2
    obj.shape

    Set obj = obj3
    obj.shape
    End Sub
    [/vba]

    It would seem to me (and you pointed it out) that in this example that you really don't need the complexity. The examples I saw used a library and books and a Collection. That would seem to me where polymorphism would be helpful. So thanks for an interesting Saturday afternoon. Much more fun than painting the bedroom.

    If you're interested, I attached the library example that I was playing with.

    I'm open to any and all suggestions

    Paul
    I'll keep looking for answers and let you know if i find one.

  15. #15
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Thanks, I'd be interested

    Even if it wasn't helpful,I learned a lot

    Paul
    Last edited by Paul_Hossler; 03-25-2012 at 07:09 AM.

  16. #16
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    What is the outstanding issue? It seems to me that Paul has already answered your question - your original calling sub should have been:
    Public Sub test() 
        Dim obj As ClsInterface 
        Set obj = New Class2
        obj.shape 
    End Sub
    Your version was calling the interface class, not one of its implementations.
    Be as you wish to seem

  17. #17
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    It would seem to me that Implements is VBA's way of trying to do OOP polymorphism (as the OP pointed out)

    In my really simple example:

    clsLibraryItem class defines the abstract interface, which clsFiction and clsHistory Implements

    Since the interface 'looks' the same between clsFiction and clsHistory, I can assign either to an object of clsLibraryItem, and the polymorphic (if that's a word) method for the appropriate type of object gets called

    [vba]
    Sub test()
    Dim i As Long
    Dim MyLibrary(1 To 4) As clsLibraryItem

    Dim FictionBook As New clsFiction
    Dim HistoryBook As New clsHistory


    Set MyLibrary(1) = New clsFiction
    Set MyLibrary(2) = New clsFiction
    Set MyLibrary(3) = New clsHistory
    Set MyLibrary(4) = New clsHistory

    MyLibrary(1).BookName = "Harry Potter (Fiction)"
    MyLibrary(2).BookName = "War of the Worlds (Fiction)"
    MyLibrary(3).BookName = "Civil War (History)"
    MyLibrary(4).BookName = "Sparta (History)"

    For i = 1 To 4
    Call MyLibrary(i).LoanTimeMsg
    Next i

    End Sub
    [/vba]

    FWIW, this is pretty heavy stuff for me I'm sure there's lots more better ways that it could be done. But sure has been interesting, I'd like to see if the pros have additional examples

    Paul
    Attached Files Attached Files

  18. #18
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    (Hit the wrong key)

  19. #19
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    (again)

  20. #20
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Basically yes, but the point is that no function code goes in the Interface class, so whilst you declare the variable as implementing that interface, you actually create a variable using the implementing class.
    Be as you wish to seem

Posting Permissions

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