Hi James,

Wow - thanks for the detail!  *Thought* I hadn't grasped something.  I'll
give those approaches a try today.  Much appreciated!

Dave.

> From: James Milne <[EMAIL PROTECTED]>
> Reply-To: REALbasic NUG <[email protected]>
> Date: Mon, 24 Apr 2006 10:28:42 +0100
> To: REALbasic NUG <[email protected]>
> Cc: REALbasic NUG <[email protected]>
> Subject: Re: How do I subclass a class with an event?
> 
> Dave Addey wrote:
>> Hi NUG,
>> 
>> I have an RB plugin, which has a GetNextDevice function that returns a new
>> instance of a class, called HIDDevice.  A HIDDevice has one event,
>> DataAvailable.  The HIDDevice class is defined in the plugin too.
>> 
>> What I need to do in RB code is to subclass the HIDDevice class, so that I
>> can enter some custom code in the subclass DataAvailable event.  This is
>> easy enough.  What I also need to do is to assign the output of
>> GetNextDevice to an instance of my subclass, and this is causing problems.
>> 
>> For example:
>> 
>> Dim myHIDDevice as HIDDevice_Mouse // a subclass of HIDDevice
>> 
>> myHIDDevice_Mouse = GetNextDevice
>> 
>> 
>> This second line fails on compilation, with the following error:
>> 
>> Type mismatch error.  Expected HIDDevice_Mouse, but got HIDDevice.
>> 
>> Now, surely, HIDDevice_Mouse is a HIDDevice.  So, shouldn¹t this work?
>> 
>> 
>> I also tried casting the output of GetNextDevice, like this:
>> 
>> myHIDDevice = HIDDevice_Mouse(GetNextDevice)
>> 
>> This compiles, but causes an IllegalCastException when this code executes in
>> the application.  I¹m not sure why.
>> 
>> How should I do this?
> 
> You can't convert one instance of a HIDDevice into a HIDDevice_Mouse. They are
> different types, even though HIDDevice_Mouse inherits from HIDDevice.
> 
> You need to separate your design into two parts. You can, for instance, use a
> Factory pattern approach. Your plugin defines a HIDDeviceFactory interface.
> You can implement that interface in classes in your REALbasic code, so that
> you have a HIDDeviceMouseFactory, HIDDeviceKeyboardFactory, etc. You create
> instances of these factories and register the instances with your plugin. The
> interface would have one method, "CreateInstance() as HIDDevice". In the case
> of the HIDDeviceMouseFactory, you would implement it like so:
> 
> Function CreateInstance() as HIDDevice
> return new HIDDeviceMouse()
> End Function
> 
> Your plugin would expose a method for registering factories with it, ie
> 
> RegisterFactory( deviceType as String, factory as HIDDeviceFactory )
> 
> When the REALbasic calls the RegisterFactory function, the plugin would add
> the factory object to an internal list of factories, along with the device
> type. When the plugin came across a HID device that matched that type, it
> would call the factory object to create an instance of the appropriate HID
> Device subtype.
> 
> Alternatively, you could split the HIDDevice class into two parts. All
> instances of HIDDevice would have a device handle property which represented
> the actual HID device. You would create instances of HIDDeviceMouse, etc on
> the app side, then attach them to the physical HID device return from the
> plugin.
> 
> Created on the REALbasic side:               Created on the plugin side:
>             HIDDevice --------------------------> HIDPhysicalDevice
>               ^
>              / \
> HIDDeviceMouse HIDDeviceKeyboard
> 
> Imagine you had a HIDDeviceManager class in the plugin  which has a
> DeviceAttached() event. You create a subclass of this in REALbasic. You would
> implement this DeviceAttached() event like so:
> 
> Class MyHIDDeviceManager
> Sub DeviceAttached( theDevice as HIDPhysicalDevice )
> select case theDevice.type
> case "Mouse"
> Dim mouse as HIDDeviceMouse(theDevice)
> ' Save mouse object somewhere
> 
> case "Keyboard"
> Dim keyboard as HIDDeviceKeyboard(theDevice)
> ' Save keyboard object somewhere
> end select
> End Sub
> 
> Hope this helps.
> 
> --
> Kind regards,
> James Milne
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
> 
> Search the archives of this list here:
> <http://support.realsoftware.com/listarchives/lists.html>


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to