VB.NET: What is an interface? How do I use IDisposable? [good understanding] [4 months]

admin / March 18th, 2011/ Posted in Software / No Comments »

Q: Im coding in vb.net for 3 ~ now, and I have a pretty of what is going on. I get to create classes / instantiating them etc, but I have no idea what interfaces are, why they exist and how to use / create. I have read many articles online cryptic and do not really get. Are they like properties within classes, or something else?

Any help / suggestions would help a lot!

Thanks,
Graham


Monthly Web Hosting
Re:Well, is Assistant an interface? Maybe you have to find a class that implements Assistant?

The One Month Magnate
Re: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.

Understanding Chart Patterns
Re:Ok, a quick google gave me this (http://weblogs.asp.net/psteele/articles/9603.aspx). Do I get bonus points now? :)

click me (http://www.google.com/search?q=VB.NET+IDisposable&ie=UTF-8&hl=xx-bork&btnG=Google-a+Seerch)


Understanding Seasonal Depression, and How To Overcome It
Re:Originally posted by: guy
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…).


Understanding Your Game Tennis eBook & Video
Re: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?)


Grow Taller 4 Idiots ~ Best Selling Grow Taller Program
Re: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.

Look Good Feel Great
Re:guy 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.


Understanding The 3 Levels of Salvation: Spirit, Soul & Body!
Re: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.


Get Good Grades In College Now
Re:Bonus points to anyone that has code for using the office assistants in .net without opening an application.

Related posts


Tags: ,

Leave a Reply

Name required

Mail (will not be published) required

Website