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 🙂
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
RealProxywhich implement interfaces dynamically etc, but again I don’t think it’s directly relevant.)