|
-
Jul 2nd, 2007, 06:37 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Implementing Interfaces?
Hi
Here is a nice simple Yes/No question, Can I use an interface like this...
This is a class with an interface, and one that implements this interface
Code:
Public class Ifaces
Public interface theInterface
Public sub doSomething()
End Interface
End Class
Public class Test
Implements Ifaces.theInterface
Public sub dosomething implements,blablabla
msgbox("Hello Interface World")
end sub
End sub
Here is the question..Its about using the object declared as the interface...
In the real instance of this application, Test will be unknown, all I know is that it will implement the interface, it will be loaded as a plugin, I just named it here as test to avoid the rest of the code for loading the assembly from which it will come from...
Code:
Dim objTest as Test
Dim obj as Ifaces.theInterface = objTest
obj.dosomething
Sorry if I am not clear enough with my explanation...
-
Jul 2nd, 2007, 08:14 PM
#2
Re: Implementing Interfaces?
Ummm....yes? Why are you declaring the Interface in a class? That one piece doesn't look right. An interface gets declared on its own. Defining it inside a class, as you have, should work, as you can define one class entirely within another, but doing so is kind of a strange thing to do. If it were declared Private, then the object (an interface in this case) would be visible, and could only be instantiated within the containing class. Since it is declared as Public, I think you could use it outside the class, but why bother defining it inside the class if you want to use it outside the class?
My usual boring signature: Nothing
 
-
Jul 2nd, 2007, 08:28 PM
#3
Thread Starter
Hyperactive Member
Re: Implementing Interfaces?
 Originally Posted by Shaggy Hiker
Ummm....yes? Why are you declaring the Interface in a class? That one piece doesn't look right. An interface gets declared on its own. Defining it inside a class, as you have, should work, as you can define one class entirely within another, but doing so is kind of a strange thing to do. If it were declared Private, then the object (an interface in this case) would be visible, and could only be instantiated within the containing class. Since it is declared as Public, I think you could use it outside the class, but why bother defining it inside the class if you want to use it outside the class?
The interface will not be public if it is declared private nomatter what the class was declared with, and I do it this way so that when someone wants to write a plugin, all they have to do to get access to all interfaces is add a refrence, which i gues it should be a namespace rather than a class in that case, thanx for pointing that out, it is kinda strange when i think about it....
-
Jul 2nd, 2007, 08:30 PM
#4
Re: Implementing Interfaces?
Right, if it was declared Private, it would BE private, which would be a REALLY odd thing to do with an interface, though it has some value when talking about classes.
My usual boring signature: Nothing
 
-
Jul 2nd, 2007, 08:37 PM
#5
Thread Starter
Hyperactive Member
Re: Implementing Interfaces?
Yeh, it would be ODD, and I misread your post before too, I thaught you said it would be private, sorry.
so is it ok to refer to an instantiated object as the type of interface it implements or i spose the name of interface it implements?
-
Jul 2nd, 2007, 08:43 PM
#6
Re: Implementing Interfaces?
The whole point of interfaces is that you CAN refer to instances of any class that implements an interface as that type. I like to think of an interface as a role that a type can fill.
My favourite example is the DataSource property of a DataGridView. You can assign any object to that property as long as it implements the IList or IListSource interface. Those interfaces expose specific members that the DataGridView will use to extract data in order to display it. The grid doesn't care what type of object it is as long as it implements those members, and the interface guarantees that it does. That is exactly why interfaces exists. Internally the DataGridView will refer to the object as an IList or IListSource, not as its actual type.
-
Jul 2nd, 2007, 08:46 PM
#7
Thread Starter
Hyperactive Member
Re: Implementing Interfaces?
 Originally Posted by jmcilhinney
Internally the DataGridView will refer to the object as an IList or IListSource, not as its actual type.
Got it, thanx...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|