Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 136,446 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,174 people online right now. Registration is fast and FREE... Join Now!




[Resolved] Polymorphism and Events (VB6)

 
Reply to this topicStart new topic

[Resolved] Polymorphism and Events (VB6)

adw888
25 Jul, 2008 - 03:43 PM
Post #1

New D.I.C Head
*

Joined: 10 Jul, 2008
Posts: 17



Thanked: 1 times
My Contributions
I know VB6 is known for having poor OOP support but basic polymorphism appears to work (at least for Subs, Functions and Properties), however my problem is that I can't work out how to raise events from the inherited classes.

I have 3 classes - clsBase, clsInheritedA, clsInheritedB.

In clsBase I have:
CODE
Option Explicit

Public Event TestEvent()

Public Sub Initialise()

End Sub

Public Property Get Initialised() As Boolean

End Property
etc.

And I want to raise the TestEvent event from clsInheritedA:
CODE
Option Explicit

Implements clsBase

Private Sub TestRaiseEvent()
    RaiseEvent TestEvent
End Sub

Private Sub Initialise()
    'do something
End Sub

Private Property Get Initialised() As Boolean
    'do something
End Property
etc.

But it gives me "Event not found" on the "RaiseEvent TestEvent" line when I try to compile it.

Does anyone know what I am missing to let the inherited class raise the events of the base class so that the host application can then respond to those events (i.e. TestEvent) when it has done:
CODE
Private WithEvents clsTest as clsBase

Then later:
CODE
Set clsTest = New clsInheritedA


Thanks in advance for any help. smile.gif

This post has been edited by adw888: 26 Jul, 2008 - 09:34 AM
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: [Resolved] Polymorphism And Events (VB6)
25 Jul, 2008 - 05:34 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
I'm not sure if this is even supported. It seems like I've run into this issue as well, and solved it by using a helper class. Unfortunately, it requires more work than the obvious way that should have been supported.


User is offlineProfile CardPM
+Quote Post

Ken Halter
RE: [Resolved] Polymorphism And Events (VB6)
25 Jul, 2008 - 07:33 PM
Post #3

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 35



Thanked: 5 times
My Contributions
I can say that firing events from implemented classes is definitely not supported. The oop support isn't so much "poor" as it is "old technology".

Agreeing with perfectly.insane (lol... that's some member name, eh? :-) that you'll need a helper class.

I have a sample on my site that shows how to get events from controls that are dynamically loaded, but not part of a control array... or even get the same event from multiple types of controls (something a lot of dotNetters think VB6 is incapable of)

Add Controls at Runtime With Events (kind of)
http://www.vbsight.com/CodeA.htm#AddControls2

It's not an extensive sample, but shows the basics and contains a lot of comments... as with every sample on my site, it was written to answer a question in a newsgroup somewhere.

If that sample doesn't help, there are many others floating around the web. My "links" page contains links to some of the most advanced VB6 programmers and samples on earth, so.... take a peek.

User is offlineProfile CardPM
+Quote Post

Ken Halter
RE: [Resolved] Polymorphism And Events (VB6)
25 Jul, 2008 - 07:47 PM
Post #4

New D.I.C Head
*

Joined: 18 Nov, 2007
Posts: 35



Thanked: 5 times
My Contributions
After re-reading, I thought I'd add...

Intellisense in VB6 is the best there is (imo)

If you type RaiseEvent(space) and your event doesn't show in intellisense, it won't work. Period. Same goes with WithEvents. If the class you need events from doesn't show after typing WithEvents(space), you won't be able to get events from that class.

In fact, while typing code, you'll find that every single variable, class, property, anything else you can think of, that's currently available to that specific procedure, will show in intellisense.

...and, if intellisense isn't showing at all, that means there's a syntax error somewhere or a missing library and the program won't compile at all... the good news there is, if you attempt to compile, since the VB6 compiler stops as soon as it can't resolve something, the line of code causing the problem will be shown in the code window and highlighted.

There is a "gotcha" there too. Some keywords (the list is very, very short) that are built deeply into the runtime won't show for some crazy reason (forgot the specifics). For example, if you type Printer(dot) you'd assume "Print" to be in the list of available properties and methods... it's not.
User is offlineProfile CardPM
+Quote Post

adw888
RE: [Resolved] Polymorphism And Events (VB6)
26 Jul, 2008 - 09:33 AM
Post #5

New D.I.C Head
*

Joined: 10 Jul, 2008
Posts: 17



Thanked: 1 times
My Contributions
Thanks for your replies perfectly.insane and Ken Halter. icon_up.gif
Your site looks particularly useful Ken, so a further thanks for the link. smile.gif

It's a shame that MS didn't do a "VB7" with the classic VB6 syntax but newer OOP support and other bugfixes; that would have been much superior to VB.NET in my opinion.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 02:47PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month