Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Dec 12, 2019 4:04 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: class interface
PostPosted: Thu May 16, 2013 3:53 pm 
Offline

Joined: Mon Oct 16, 2006 4:06 pm
Posts: 110
Location: Fort Worth, TX
I have a subclass of a shell (vdl_main). I'd like data returned from the shell to be displayed in my subclassed textarea (vdl_textarea). Rather than having vdl_main mention my instance of vdl_textarea explicitly, I was hoping there was more of a generic, reusable way to get information from one class to another. I poked around and it looks like a class interface is what I'm looking for, but something's not working for me.

I created a class interface vdl_display with the method set_text. I created a corresponding method set_text on vdl_textarea. I applied the class interface vdl_display to vdl_textarea. I was hoping that at this point in my vdl_main class I could just say:

vdl_display.set_text("blah blah blah")


and any class with the vdl_display interface would run its corresponding set_text function. However, when I try and run this, the vdl_display.set_text part of the line is highlighted with the error "item does not exist." Interestingly, vdl_display.set_text auto completes when typing it out so I thought I was on the right path.

If someone could help me understand class interfaces a little better I would greatly appreciate it.


Top
 Profile  
Reply with quote  
 Post subject: Re: class interface
PostPosted: Thu May 16, 2013 4:38 pm 
Offline
User avatar

Joined: Mon Feb 05, 2007 5:21 pm
Posts: 600
Location: New York, NY
You're on the right track, but your vdl_main class still has to have an instance of a class to work with, whether that's an actual class or an interface.

The Interface is just a way of standardizing methods among otherwise-unrelated classes. Within the code, it's a way of accepting and working with these classes where all you know about them is the methods that the interface implements. You can't instantiate a class interface directly, e.g., dim x as new class_interface, but otherwise would treat them the same way you would a class.

In your case, let's take the Interface out of it for a moment. The TextArea class has a Text property, but you couldn't put "TextArea.Text = something" into your code and expect it to work. It needs to know which instance of a TextArea you mean. Likewise, when working with an Interface, it needs to know which instance you mean too.

But I like the way your are going with this. Later, you might want your vdl_main class to write to something else like a Label or even a file, so abstracting it with an Interface is a good idea. What I'd do is create a computed property that stores the vdl_display as a WeakRef. When it comes time to write to that, it converts the WeakRef back to a vdl_display and, if it's not nil, writes to it using set_text.

Let me know if you need an example of this.

_________________
Kem Tekinay
MacTechnologies Consulting
http://www.mactechnologies.com/

Need to develop, test, and refine regular expressions? Try RegExRX.


Top
 Profile  
Reply with quote  
 Post subject: Re: class interface
PostPosted: Thu May 16, 2013 4:40 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
I don't think an interface is the right solution for this problem. You should use an Event instead. Add an event to the Shell subclass

Event Notify(message as string)

and call it from the shell subclass code. Then you can put an instance of your shell subclass on the window and fill out the Notify event, where it is perfectly acceptable to refer to the textarea directly. Or you can create a shell instance in code and use AddHandler to point the Notify event to a window method. Again, that method can address the textarea by name. Either way it would look like

vdl_textarea.Text = message


Top
 Profile  
Reply with quote  
 Post subject: Re: class interface
PostPosted: Thu May 16, 2013 4:53 pm 
Offline

Joined: Mon Oct 16, 2006 4:06 pm
Posts: 110
Location: Fort Worth, TX
Thank you, now I understand the item not existing issue. In my head I'm envisioning my vdl_main class as a radio station broadcasting on a specific frequency to no one in particular. If a listener (other class) hears something on their channel they act on what they hear. Might be a rather farfetched idea.

I'll play around with both your suggestions and see what I can figure out.


Top
 Profile  
Reply with quote  
 Post subject: Re: class interface
PostPosted: Thu May 16, 2013 5:32 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Class interfaces cannot be used to "broadcast" a method call to all implementers of the interface. A class interface merely defines the names and parameters/return value of a collection of methods which actual classes can implement. An interface specifies what a class does without specifying how it does it.

The purpose of class interfaces is to allow multiple, distinct classes, who have no other relationship to one another but whose purposes overlap or are generically similar, to be manipulated by the same generic code. For example, both the TextOutputStream class and the TCPSocket class implement the Writeable interface. Because they both implement the same interface, code like this is possible:

Dim MyWriteable As Writeable ' Create an instance of the interface
If Microseconds Mod 2 = 0 Then
MyWriteable = New TCPSocket
Else
MyWriteable = TextOutputStream.Create(SpecialFolder.Desktop.Child("hello.txt"))
End If
'we don't need to know whether we're operating on a socket or a file
'here since we're using the class interface
MyWriteable.Write("Hello, world!")


These two classes are fundamentally different in how they do what they do, but can be handled by generic code since they both implement the same class interface.

Tim's suggestion about using events is a good one, I think.

_________________
Boredom Software


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group