VB.NET: What is an interface? How do I use IDisposable?

gsiener

Senior member
Jun 14, 2000
436
0
0
I've been coding in vb.net for 3~4 months now, and I have a pretty good understanding of everything that's going on. I get making classes/instantiating them etc, but I have no clue as to what interfaces are, why they exist, and how to use/make them. I've read lots of cryptic articles online and don't really get it. Are they like properties within classes, or something else?

Any help/suggestions would help a ton!

Thanks,
Graham
 

gsiener

Senior member
Jun 14, 2000
436
0
0
Bonus points to anyone that has code for using the office assistants in .net without opening an application.
 

tkdkid

Senior member
Oct 13, 2000
956
0
0
An interface is a pre-defined list of functions. If a class implements an interface, it must have all of the functions of that interface.

They are useful for writing generic code. Say for example that you have a function that populates a listbox. The function takes one parameter, which is of type ListBoxItem. ListBoxItem is your interface. You can pass in any type of object as long as it implements the ListBoxItem interface.

Because the object implements the interface, you can rely on certain functions being defined. For example, the ListBoxItem interface will have a toString function that will return a string that represents the object. You can use the string to populate the listbox.

So, by using an inteface, you can pass in all kinds of objects and they will get displayed correctly in your listbox. This is a simple example, there are many cool things that you can do with interfaces, mainly focusing on the idea of writing generic code that can work with many types of objects.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
tkdkid speaks the truth. Here, also, is my interpretation.
An interface is very similar to a completely abstract class. All interface, no implementation. The reason is to loosen the rigidities of the single inheritance class model. If you have two classes in different inheritance trees but you have code that needs to handle both without knowing the difference then making the two disparate classes conform to a single interface allows them to be treated the same. Interfaces let one class take on multiple roles and let a single role be filled by one of a large number of classes.

Sometimes (like in Delphi) interfaces can also be used to represent objects that are not defined in the language. For instance, the base interface IUnknown in Delphi represents an object from the windows environment (presumably written in C++) and defines certain methods that can be performed on such objects. Other interfaces extend IUnknown to allow more access to the foreign objects.
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
Like they said, it's just a way to make sure that a class has certain methods or properties. If a class implements an interface, it must have certain methods or properties in it. The nice thing about interfaces is that you can implement as many as you want.
 

gsiener

Senior member
Jun 14, 2000
436
0
0
Hmm. Well that's a good start. Thanks so much, I'll play around and see if I can get a better grasp!

(as an aside, if I want to use IDisposable in a class I made, do I just put implements IDisposable in the constructor?)
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: gsiener
Hmm. Well that's a good start. Thanks so much, I'll play around and see if I can get a better grasp!

(as an aside, if I want to use IDisposable in a class I made, do I just put implements IDisposable in the constructor?)

You implement IDisposable if you want your class to be able to act like a Disposable object. In order to compile you'd then have to implement all the methods that are in the IDisposable interface. If you have other classes that implement IDisposable already (like something in the api) you can write code that works with an IDisposable object just like a regular object. You can then call only the methods that are defined in the interface.

I hope you have fun learning about interfaces! Once you realize some of the things you can do with them they become a very powerful tool. I can't speak for .NET but a huge part of the Java api relies on interfaces, especially the enterprise and vendor specific stuff (xml, databases...).
 

gsiener

Senior member
Jun 14, 2000
436
0
0
Yeah, I've been searching google for a while (think goooooooogle) and saw some stuff, but nothing cut and dry. That article is pretty good, I found another msdn article that was practical as well. As for the bonus points, they were only using office assistants in .NET. I've found some page that says how to call them, but after referencing Microsoft.Office.Core and dim'ing Assistant, I get object reference not set to an istance of an object. Only I can't New Assistant, so I decided to stop trying.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Well, is Assistant an interface? Maybe you have to find a class that implements Assistant?