New topic: 

Sharing common code between disparate subclasses

<http://forums.realsoftware.com/viewtopic.php?t=46432>

         Page 1 of 1
   [ 5 posts ]                 Previous topic | Next topic          Author  
Message        bachya          Post subject: Sharing common code between 
disparate subclassesPosted: Fri Jan 04, 2013 12:57 am                         
Joined: Wed Dec 20, 2006 1:23 pm
Posts: 5                I have two separate classes (A = a subclass of 
BevelButtom, B = a subclass of PushButton). Both A and B implement a number of 
identical methods in exactly the same way. Since both subclass' superclasses 
are different and since RB doesn't support multiple inheritance, all I can do 
to tie these methods together is define a class interface, have both subclasses 
implement the interface, then copy/paste the method bodies in each subclass.

That offends my sensibilities. Is there a way in RB to extract this common 
logic elsewhere?

Thanks!   
                             Top                timhare          Post subject: 
Re: Sharing common code between disparate subclassesPosted: Fri Jan 04, 2013 
1:36 am                         
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 11944
Location: Portland, OR  USA                The fact that the methods are 
exactly the same suggests that they shouldn't be part of the controls at all.  
Perhaps they should be utility methods in a Module.  The Class interface would 
then define a method that the utility method calls to perform some 
class-specific action.  The utility method would accept a parameter of the 
class interface type, do its thing and then call the class method to handle the 
result.   
                             Top                Thom McGrath          Post 
subject: Re: Sharing common code between disparate subclassesPosted: Fri Jan 
04, 2013 1:48 am                       Site Admin                
Joined: Tue May 06, 2008 1:07 pm
Posts: 1318
Location: Greater Hartford Area, CT                Create the interface, but 
don't add any methods. On a module, you can define methods which include the 
Extends parameter keyword on the interface name.

So let's say your interface is called ButtonParent. You can create a global 
method in a module such as

Sub Foo (Extends Button As ButtonParent)
And now you can simply call PushButton.Foo. Be aware though that in your 
implementation code, you cannot call any methods or properties of button 
because it is now generic, so you have to use TypeCasting. This makes your code 
look something like:

Sub Foo (Extends Button As ButtonParent)
  If Button IsA PushButton Then
  Dim ButtonPush As PushButton = PushButton(Button)
  MsgBox(ButtonPush.Caption)
  ElseIf Button IsA BevelButton Then
  Dim ButtonBevel As BevelButton = BevelButton(Button)
  MsgBox(ButtonBevel.Caption)
  End If
End Sub

So as you can see, you'll still need to separate your code in some way. Only 
you will know if this technique will actually help you.      
_________________
Thom McGrath - @tekcor
Web Framework Architect, Real Software, Inc.  
                             Top                bachya          Post subject: 
Re: Sharing common code between disparate subclassesPosted: Fri Jan 04, 2013 
8:41 am                         
Joined: Wed Dec 20, 2006 1:23 pm
Posts: 5                Thank you both. I will give this a shot and report back 
whether it satisfies my needs.   
                             Top                bachya          Post subject: 
Re: Sharing common code between disparate subclassesPosted: Fri Jan 04, 2013 
1:50 pm                         
Joined: Wed Dec 20, 2006 1:23 pm
Posts: 5                Hi again,

I was able to get Paul's eyes on this, as well (via StackOverflow: 
http://stackoverflow.com/questions/1415 ... s/14152440) and he provided some 
additional insight.

Thanks to all who offered assistance. I should be able to craft a more elegant 
solution w/ everyone's input.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 5 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to