Hi,
I've wrote a small class called Car and attached an interface iCar. So far,
so good....
What i would like to know is ... What is the correct use for the
interface...?
sub Drive(myCar as iCar){
}
... Works great....
However...
Dim myCar as new iCar
doesn't work....
Kind regards
John
As Seth demonstrated, you still instantiate your object the normal way
- i.e. using the constructor of the Car class.
Just remember that an interface is a contract of sorts. If you
implement an interface on a class, you are guarenteeing that this
class implements the properties and methods of the interface. You
cannot instantiate an object from an interface directly.
So, when your car class implements the iCar interface, you are saying
that your car class implements those properties and methods defined on
the iCar interface. The Car instance (myCar in your example) is
considered to be of type iCar (as well as type Car, as well as type
object and as well as any other classes that you have inherited from).
So you can pass the instance of myCar to a sub that takes parameters
of type: Car, iCar and Object (or if you have inherited from another
class).
In the sub Drive you have defined, remember that when you pass your
myCar instance to it, you will only have access to the properties and
methods defined on the iCar interface (because sub Drive takes an
object of type iCar). Any other properties and methods you have
defined on the Car class beyond those specified on the iCar interface
you will not have access to (unless you do a type conversion - but
then you risk the possibility of an exception being thrown if you pass
an object not of the type you are converting to).