April 25th, 2006, 02:04 PM
VB.NET/C#: Require Usage of Interfaces
I am looking for a way to have my developers (C#, VB.NET) to
use classes via interfaces only. I do not want to achieve this
using management processes (development policies), but via
a specific implementation of classes.
I present a way how to do this, however, I am not completely
happy with it.
An example (in VB.NET, because this requirement is more of an issue here):
Public Interface IClass1
Function IWhoAmI() As String
Public Class Class1
Protected[ Friend]/Private Function WhoAmI() As String Implements IClass1.IWhoAmI
Return "Class: Class1 "& vbCrLf &" Interface: IClass1"
I can use this class/interface as follows:
' 1. Way:
Dim Test_I As IClass1
Test_I = New Class1
Dim Test_C As Class1
Test_C = New Class1
Test_C... ' <-- empty, no function available
Thus, using this approach I need the class Class1 Public (or at least
Friend [default]), as well as the New-operator. The nice thing here is,
however, that the Test_C-object does not allow to use any function
(fine, so far).
But I am not happy since this requires that my class Class1 has
no public elements. This is achieved by design, however there are
people coding. Hence, ...
I would like to have a way, which does not allow the "2.Way".
Any help is appreciated.
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)