What is the difference of using interfaces like …
I have an interface
Public Interface IProDataSource
Function read() As Integer
End Interface
Then a class
Public Class DataSource : Implements IProDataSource
Function read() As Integer Implements IProDataSource.read
some code...
End Function
End Class
Then I use this the next way … but what is the difference? … both approaches are working …
Dim obj As IProDataSource = New DataSource
obj.read()
vs
Dim datas as new Datasource
datas.read()
The only difference I have notice is that if the method is declare private it will be visible using the first approach only.
Thanks for any comments !!!
If
objis aDataSource, you cannot get to the non-Interface methods of the DataSource through a variable declared as the interface. Otherwise, in your example, there is really no difference.Notice the “is a”.
IListis a good interface to check out. Many objects take or useIListas method parameters. (To see all the classes that implementIList, open the “Object Browser”, browse toIListand expand “Derived Types”.) For example, if I was implementing a Windows.Forms control, instead of forcing you to pass a list as anArray,List,Collection, or some other object, I could specifyIListand any class that implements that interface can be used by the control.A practical example, the
DataSourceproperty of aComboBoxaccepts any object that implements theSystem.Collections.IListinterface, such as aSystem.Data.DataSetor anSystem.Array.http://msdn.microsoft.com/en-us/library/s3et34z3(VS.80).aspx