tags:

views:

315

answers:

6

Let me explain. I have defined an interface named IEmployee (ID, FirstName and Surname properties) but I have not yet implemented that interface in any class.

What I want to do is something like:

Dim User as New IEmployee
User.ID = 1
User.FirstName = ""
User.Surname = ""

Call SomeFunction (User)

The VB.NET complier does not seem to like this.

EDIT** Thanks all, all good points well made! I'll instantiate a concrete class. I was just trying to take a shortcut :)

+8  A: 

No, it wouldn't. You can't create instances of interfaces directly like this: what would you expect the property calls to do? Property calls invoke code - but you haven't provided an implementation. Suppose you'd define a method in the interface to calculate the employee's holiday allowance: what would you expect that to do if you called it, when you haven't implemented the interface anywhere?

The point of an interface is to specify an API that is then implemented in concrete classes - you can't use the interface without something implementing it properly.

(Note that I think there are some really weird situations in which the C# compiler and possibly the VB one will allow you to instantiate interfaces directly, but I think they're COM-specific situations. I assume this isn't to do with COM. There are also things like RealProxy which implement interfaces dynamically etc, but again I don't think it's directly relevant.)

Jon Skeet
as far as Interfaces used as DTOs (just property/field carriers) this would be incredibly useful, I don't know if RealProxy, or Marc's Proxy would do it, but LinFu certainly appears to do this, or maybe it's some magic in AutoMapper. Either way I'm able to generate an instance of an interface using AutoMapper that appears to rely on LinFu for that work.
Maslow
A: 

No. An interface is just a contract between a class that implements it and the rest of the world. When code talks about an interface, it's really talking about a concrete piece of code that has "signed" that contract. Just as the employee signing an employment contract binds them to what they can do, implementing an interface binds the class. You can't sit the contract document in front of a screen to type in place of the human, and you can't ask an interface execute any functionality.

You'll have to pick a concrete implementation, and instantiate that.

Adam Wright
A: 

You really need to implement the interface before you can use it. An interface is just a description of how an object could act - you cannot interact with a description, you really need a concrete implementation running behind it.

1800 INFORMATION
A: 

Interfaces are definitions of what classes should look like and therefore dont have any code implemented but if youreally want You can use mocking, which under the covers creates a proxy class that implements the interface so you can then call it,

Since the item is an interface and by definition has no implementation its usually a good idea to set expectations for each property/method

http://ayende.com/Blog/archive/2008/06/29/Rhino-Mocks-3.5-Release-Candidate.aspx is the link

almog.ori
+2  A: 

There are very few things you can usefully do with an interface without a concrete implementation. You certainly can't create anything...

The only useful way I've found is (with Expression) expressing intent, for example with RPC:

(aside)

new Proxy<ISomeService>().Invoke(svc => svc.SomeMethod(123));

This says if we had an ISomeService instance "svc" (which we don't), then we'd call SomeMethod passing in 3; we can then pull the Expression apart to find this out.

In all other scenarios - if you don't have a concrete implementation either at compile-time or runtime (dynamic type generation), then it isn't any use whatsoever.

Marc Gravell
as far as Interfaces used as DTOs (just property/field carriers) this would be incredibly useful, I don't know if Proxy, or Jon's RealProxy would do it, but LinFu certainly appears to do this, or maybe it's some magic in AutoMapper. Either way I'm able to generate an instance of an interface using AutoMapper that appears to rely on LinFu for that work.
Maslow
A: 

LinFu appears to be able to do this. It will generate a type using Reflection.Emit that would accept your sample values. But method implementation if the interface has methods and you wish them to function would have to be fed in the same way you are feeding in properties/fields.

Maslow