Real Software Forums

class interface
Page 1 of 1

Author:  griffitts [ Thu May 16, 2013 3:53 pm ]
Post subject:  class interface

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.

Author:  ktekinay [ Thu May 16, 2013 4:38 pm ]
Post subject:  Re: class interface

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.

Author:  timhare [ Thu May 16, 2013 4:40 pm ]
Post subject:  Re: class interface

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

Author:  griffitts [ Thu May 16, 2013 4:53 pm ]
Post subject:  Re: class interface

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.

Author:  charonn0 [ Thu May 16, 2013 5:32 pm ]
Post subject:  Re: class interface

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
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.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group