Help me, I Confuse

dragon4spy

Well-known member
Joined
Dec 22, 2003
Messages
110
Hi everyone! :) Could you tell me the differences between Structure, Interface, Delegate, and Class? It seems that these things are now (vb.net) capable to handle methods and functions as well.

Is there any more objects that similar to Class?:D
 
A Structure is much like the Type in VB6... you dont really have to instantiate it before you use it... you can in .NET, and you can also have Methods (Functions and Subs) within the class, also Shared methods and good stuff like that... it does pretty much what a class would do except the object-oriented stuff like Inheritance, Implementation, etc. :cool:

An Interface is something that you add into a class... within the class, you can implement them to have some functionality...
An interface is kind of like a plug-in socket to some device... you specify what the socket looks like (a 3-pronged wall socket has a source power, a source ground, and a body ground... those are what would be in the interface)... when implemented in the class, you get to do stuff with the source power, source ground, and body ground. :eek:
In the Interface all of the subs and functions are just declared... nothing is added.

A Delegate is like an Event Handler... well, it is an event handler.
You can declare a delegate like this:
Public Delegate Sub MvPtclListExitEventHandler(ByVal sender As Object, ByVal e As MvPtclListExitEventArgs)
This is moving particle list exit event handler.

So, in a class, I have moving particles which all must lie within some rectangle ... I instruct them to move in MoveSubroutine()...
then I check the particle location... if they have moved outside of the rectangle, then I can raise the event handler:
Public Event OutOfBounds As MvPtclListExitEventHandler
which is declared at the declarations part of my class.
When I RaiseEvent OutOfBounds(Me, mpleeargs), the delegate "gets in the car and drives", looking for places where it can be handled in the project.
So, I happen to be handling it in an event on a form
Private Sub Swarm_OutOfBounds(ByVal sender As Object, ByVal e As MvPtclListExitEventArgs) Handles Swarm.OutOfBounds
The delegate "pulls into the driveway", drops the arguments (Me, mpleeargs) from the class... and then the event runs. :D

Also, MvPtclListExitEventArgs is a class which Inherits System.EventArgs
and you can add methods and properties to it. :)
 
Interfaces allow a framework to provide placeholders / plug in mechanism when the framework knows what functionality is required but not how a given class needs to implement it.
e.g. Have a look at Array.Sort and the IComparable interface.
 
Is there something wrong with the detailed explanations on these topics that MSDN gives you?
 
You can simply search for something like IComparable or IFormatter with the Documentation Help Search. For instance, assume I have a vector class that implements IFormattable. IFormattable provides you with an empty ToString() function for your class.
Sure, you can just call it directly
MyVector.ToString()
but, you can also use it here:
Console.WriteLine("My vector is {0}.", MyVector)
and it will call my .ToString() property of My vector to determine what to put in place of the {0}.
 
Back
Top