Interface

R

rn5a

Suppose I have the following class code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class DBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class

Now all the code that exists in the above class (between the lines
Public Class DBSettings & End Class) - is that what is called the
interface?

If I am wrong, can someone please explain me what exactly is an
interface?

Thanks
 
R

rn5a

Suppose I have the following class code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class DBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class

Now all the code that exists in the above class (between the lines
Public Class DBSettings & End Class) - is that what is called the
interface?

If I am wrong, can someone please explain me what exactly is an
interface?

Thanks

I mean what exactly is an interface as far as object-oriented
programming is concerned?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Suppose I have the following class code:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class DBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class

Now all the code that exists in the above class (between the lines
Public Class DBSettings & End Class) - is that what is called the
interface?
No.

If I am wrong, can someone please explain me what exactly is an
interface?

An interface is a contract that a class can implement. It can contain
the defintions of methods and properties, but no methods and no data.

You could for example create this interface, that your DBSettings class
could implement:

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

The point is that you can write code that makes use of the interface,
without caring what the actual class is that implements it. That in turn
means that you can write that code even before there is a class that
implements the interface, and also that you can create different classes
that implement the interface.
 
B

bruce barker

an interface is a contract of method signatures defined with an
abstract class (defines methods only). ain turn class specifies that it
will implement the interface.

as your sample does not specify any interfaces that it will implement,
it has none. now assume interface:

// c# - don't know vb well enough
public interface IQuery
{
SqlDataReader QueryDB(string q);
}

and you changed your code to:

Public Class DBSettings implements IQuery

then your class would have implemted interface IQuery and could be cast
as an IQuery.

-- bruce (sqlwork.com)
 
C

Cowboy \(Gregory A. Beamer\)

For your example, there is no explicit interface, but if there were one it
would be

Public Interface IDatabase
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

Your class, to use this interface, would be

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class DBSettings Implements IDatabase
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class

I may have this a bit off, as I normally work in C#, but the basics are
there.
 
R

rn5a

An interface is a contract that a class can implement. It can contain
the defintions of methods and properties, but no methods and no data.

You could for example create this interface, that your DBSettings class
could implement:

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

The point is that you can write code that makes use of the interface,
without caring what the actual class is that implements it. That in turn
means that you can write that code even before there is a class that
implements the interface, and also that you can create different classes
that implement the interface.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Goran, you have said that an interface can have only the definitions
of methods & properties but no methods & no data. So is this how I
change the code shown in post #1 to make use of an interface?

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

Public Class DBSettings Implements IDBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class
The point is that you can write code that makes use of the interface,
without caring what the actual class is that implements it. That in turn
means that you can write that code even before there is a class that
implements the interface, and also that you can create different classes
that implement the interface.

I guess you have said the above from the programmer's point of view.
If the code I have shown above in this post that uses the interface is
correct, I couldn't follow what do you mean by "you can write code
that makes use of the interface without caring what the actual class
is that implements it". Could you please be a bit more specific?

Also what if I omit the words "Implements IDBSettings" from the line
"Public Class DBSettings Implements IDBSettings"? Won't I be allowed
to create an instance of the class using

Dim dbs As DBSettings

dbs = New DBSettings

If I will be allowed to create an instance of the class DBSettings,
then why use an interface in the first place? Under what circumstances
should an interface be used?

Also the code shown in post #1 - that is just the IMPLEMENTATION,
isn't it?

I am not sure if I have expressed my doubts accurately or not but if
yu (& others) are finding it difficult to understand what I am trying
to say, please do let me know. I will try to be as lucid as possible.

Thanks to all of you. One request please - if showing code, please try
to use VB & not C# as I am not fluent with C#.
 
R

rn5a

An interface is a contract that a class can implement. It can contain
the defintions of methods and properties, but no methods and no data.

You could for example create this interface, that your DBSettings class
could implement:

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

The point is that you can write code that makes use of the interface,
without caring what the actual class is that implements it. That in turn
means that you can write that code even before there is a class that
implements the interface, and also that you can create different classes
that implement the interface.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -

Goran, you have said that an interface can have only the definitions
of methods & properties but no methods & no data. So is this how I
change the code shown in post #1 to make use of an interface?

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

Public Class DBSettings Implements IDBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class
The point is that you can write code that makes use of the interface,
without caring what the actual class is that implements it. That in turn
means that you can write that code even before there is a class that
implements the interface, and also that you can create different classes
that implement the interface.

I guess you have said the above from the programmer's point of view.
If the code I have shown above in this post that uses the interface is
correct, I couldn't follow what do you mean by "you can write code
that makes use of the interface without caring what the actual class
is that implements it". Could you please be a bit more specific?

Also what if I omit the words "Implements IDBSettings" from the line
"Public Class DBSettings Implements IDBSettings"? Won't I be allowed
to create an instance of the class using

Dim dbs As DBSettings

dbs = New DBSettings

If I will be allowed to create an instance of the class DBSettings,
then why use an interface in the first place? Under what circumstances
should an interface be used?

Also the code shown in post #1 - that is just the IMPLEMENTATION,
isn't it?

I am not sure if I have expressed my doubts accurately or not but if
yu (& others) are finding it difficult to understand what I am trying
to say, please do let me know. I will try to be as lucid as possible.

Thanks to all of you. One request please - if showing code, please try
to use VB & not C# as I am not fluent with C#.
 
R

rn5a

Goran, you have said that an interface can have only the definitions
of methods & properties but no methods & no data. So is this how I
change the code shown in post #1 to make use of an interface?

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

Public Class DBSettings Implements IDBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class


I guess you have said the above from the programmer's point of view.
If the code I have shown above in this post that uses the interface is
correct, I couldn't follow what do you mean by "you can write code
that makes use of the interface without caring what the actual class
is that implements it". Could you please be a bit more specific?

Also what if I omit the words "Implements IDBSettings" from the line
"Public Class DBSettings Implements IDBSettings"? Won't I be allowed
to create an instance of the class using

Dim dbs As DBSettings

dbs = New DBSettings

If I will be allowed to create an instance of the class DBSettings,
then why use an interface in the first place? Under what circumstances
should an interface be used?

Also the code shown in post #1 - that is just the IMPLEMENTATION,
isn't it?

I am not sure if I have expressed my doubts accurately or not but if
yu (& others) are finding it difficult to understand what I am trying
to say, please do let me know. I will try to be as lucid as possible.

Thanks to all of you. One request please - if showing code, please try
to use VB & not C# as I am not fluent with C#.- Hide quoted text -

- Show quoted text -

Thanks, Gregory, for your response. From your post, I can conclude
that the code I have shown in my previous post is correct. So again
the same question - why make use of an interface in the first place?

Ron
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Goran, you have said that an interface can have only the definitions
of methods & properties but no methods & no data. So is this how I
change the code shown in post #1 to make use of an interface?

Public Interface IDBSettings
Public Function QueryDB(qry As String) As SqlDataReader
End Interface

Public Class DBSettings Implements IDBSettings
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection
Public ConnectionString As String

Public Function QueryDB(qry As String) As SqlDataReader
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader

sqlConn.Close()
End Function
End Class

Yes, that defines an interface and a class that implements the interface.

(The method is unusable, though, as the database connection has to be
open while you read from the data reader.)
I guess you have said the above from the programmer's point of view.
If the code I have shown above in this post that uses the interface is
correct, I couldn't follow what do you mean by "you can write code
that makes use of the interface without caring what the actual class
is that implements it". Could you please be a bit more specific?

You can for example write a method that uses the interface, and doesn't
care about the class implementing it:

Public Function GetInfo(settings as IDBSettings) as SqlDataReader
Return settings.QueryDB("select something from sometable")
End Function
Also what if I omit the words "Implements IDBSettings" from the line
"Public Class DBSettings Implements IDBSettings"? Won't I be allowed
to create an instance of the class using

Dim dbs As DBSettings

dbs = New DBSettings

Yes, you would. If the class implements the interface, you can do this:

Dim dbs as IDBSettings

dbs = New DBSettings()

Now you have a reference to the interface, but it points to an instance
of the class.
If I will be allowed to create an instance of the class DBSettings,
then why use an interface in the first place? Under what circumstances
should an interface be used?

An interface is usually used to specify some behaviour. You can for
example use the IComparer interface to specify that a class is capable
of comparing two values, and then use that class in a call to Array.Sort
to provide tha comparison method for the sorting.
 
R

rn5a

Yes, that defines an interface and a class that implements the interface.

(The method is unusable, though, as the database connection has to be
open while you read from the data reader.)



You can for example write a method that uses the interface, and doesn't
care about the class implementing it:

Public Function GetInfo(settings as IDBSettings) as SqlDataReader
Return settings.QueryDB("select something from sometable")
End Function




Yes, you would. If the class implements the interface, you can do this:

Dim dbs as IDBSettings

dbs = New DBSettings()

Now you have a reference to the interface, but it points to an instance
of the class.


An interface is usually used to specify some behaviour. You can for
example use the IComparer interface to specify that a class is capable
of comparing two values, and then use that class in a call to Array.Sort
to provide tha comparison method for the sorting.

--
Göran Andersson
_____http://www.guffa.com- Hide quoted text -

- Show quoted text -
(The method is unusable, though, as the database connection has to be
open while you read from the data reader.)

Goran, why is the method unusable? I have explicitly opened the
database connection to read from the data reader.
Dim dbs as IDBSettings

dbs = New DBSettings()

Now you have a reference to the interface, but it points to an instance
of the class.

But why would a programmer do something like what you have shown
(reference to an interface but pointing to an instance of a class)
when the same can be done without referencing the interface like this?

Dim dbs As DBSettings
dbs = New DBSettings()
An interface is usually used to specify some behaviour. You can for
example use the IComparer interface to specify that a class is capable
of comparing two values, and then use that class in a call to Array.Sort
to provide tha comparison method for the sorting.

If I am not mistaken, a class is also used to specify some behavior
(please correct me if I am wrong). A class comparing 2 values can also
be created without using the IComparer interface. So why use an
interface?

BTW, when I use the code that I have shown in my previous post (which
has the interface & the class that implements the interface) in a
class (.vb) file (removing the "Public" keyword while declaring the
method QueryDB in the interface since Public is not valid on interface
method declaration), then the error "End of statement expected" gets
generated pointing to the line shown below:

Public Class DBSettings Implements IDBSettings

What's causing that error?

Sorry for the follow-up questions Goran but I am not able to
understand what's the use of interfaces or why use them in ASP.NET.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Goran, why is the method unusable? I have explicitly opened the
database connection to read from the data reader.

You open the connection, create the reader, then close the connection.
As the reader needs the connection to be open, it unusable after the
connection has been closed. When the method returns the reader, it's no
longer possible to read anything from it.
But why would a programmer do something like what you have shown
(reference to an interface but pointing to an instance of a class)
when the same can be done without referencing the interface like this?

Dim dbs As DBSettings
dbs = New DBSettings()

It's just an example to show that the reference can be a reference to an
interface instead of a reference to the class. It's of course only
useful when one method creates the object and another method uses the
object (in the shape of the interface).
If I am not mistaken, a class is also used to specify some behavior
(please correct me if I am wrong).

A regular class just have some behaviour, it doesn't define behaviour
for other classes.

An abstract class on the other hand, can both define behaviour for
derived classes, and implement behaviour itself. An abstract class that
doesn't implement anything itself is similar to an interface.
A class comparing 2 values can also
be created without using the IComparer interface. So why use an
interface?

Yes, you can make a class that compares two values, but you can't make
the Array.Sort aware of the fact that the class can do this if you don't
use the interface.
BTW, when I use the code that I have shown in my previous post (which
has the interface & the class that implements the interface) in a
class (.vb) file (removing the "Public" keyword while declaring the
method QueryDB in the interface since Public is not valid on interface
method declaration),

Right. I missed to remove the access modifier in the interface. You
can't specify any access level in an interface as everything is always
public.
then the error "End of statement expected" gets
generated pointing to the line shown below:

Public Class DBSettings Implements IDBSettings

What's causing that error?

You have to put them on separate lines:

Public Class DBSettings
Implements IDBSettings

You also have to specify Implements on the method:

Public Function QueryDB(ByVal qry As String) As SqlDataReader Implements
IDBSettings.QueryDB

(I usually do this in C#, that isn't line based, and that just
identifies the methods for the implementation by name.)
 
R

rn5a

You open the connection, create the reader, then close the connection.
As the reader needs the connection to be open, it unusable after the
connection has been closed. When the method returns the reader, it's no
longer possible to read anything from it.




It's just an example to show that the reference can be a reference to an
interface instead of a reference to the class. It's of course only
useful when one method creates the object and another method uses the
object (in the shape of the interface).



A regular class just have some behaviour, it doesn't define behaviour
for other classes.

An abstract class on the other hand, can both define behaviour for
derived classes, and implement behaviour itself. An abstract class that
doesn't implement anything itself is similar to an interface.


Yes, you can make a class that compares two values, but you can't make
the Array.Sort aware of the fact that the class can do this if you don't
use the interface.


Right. I missed to remove the access modifier in the interface. You
can't specify any access level in an interface as everything is always
public.




You have to put them on separate lines:

Public Class DBSettings
Implements IDBSettings

You also have to specify Implements on the method:

Public Function QueryDB(ByVal qry As String) As SqlDataReader Implements
IDBSettings.QueryDB

(I usually do this in C#, that isn't line based, and that just
identifies the methods for the implementation by name.)

Goran, I am highly obliged to you for putting in so much effort & time
in trying to make me understand interfaces. I really appreciate it
from the bottom of my heart (& I really mean it). Now I am getting a
hang of interfaces & their uses. I guess you must be pleased to know
that your efforts aren't being wasted!

Finally I could make that code work. This is the class (.vb) code (I
am reproducing the entire code so that someone else can benefit from
it in the future; after all everyone won't be as fortunate as me to
have a helpful guide like you):

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Interface IDBSettings
Property ConnectionString() As String
Function QueryDB(ByVal qry As String) As SqlDataReader
End Interface

Public Class DBSettings
Implements IDBSettings
Public ConnString As String
Private sqlCmd As SqlCommand
Private sqlConn As SqlConnection

Public Property ConnectionString() As String Implements
IDBSettings.ConnectionString
Get
ConnectionString = ConnString
End Get
Set(ByVal value As String)
ConnString = value
End Set
End Property

Public Function QueryDB(ByVal qry As String) As SqlDataReader
Implements IDBSettings.QueryDB
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader
sqlConn.Close()
End Function
End Class

& this is the ASPX code:

<%@ Page Language="VB" Explicit="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim boDBSettings As IDBSettings
Dim sqlDReader As SqlDataReader

boDBSettings = New DBSettings
boDBSettings.ConnectionString = ".........."
sqlDReader = boDBSettings.QueryDB("SELECT * FROM Table1")

If Not (sqlDReader Is Nothing) Then
dg1.DataSource = sqlDReader
dg1.DataBind()

sqlDReader.Close()
End If
End Sub
</script>
<form runat="server">
<asp:DataGrid ID="dg1" runat="server"/>
</form>

There is one thing I noticed when I made the reference to the
IDBSettings interface (instead of the DBSettings class) in the ASPX
code. I use Visual Web Developer 2005 for ASP.NET apps. When I typed

boDBSettings.

(notice the dot after boDBSettings), then VWD's intellisense lists
only 2 items - the property named ConnectionString & the function
named QueryDB but if I make the reference to the DBSettings class,
then apart from ConnectionString & QueryDB, VWD's intellisense also
lists other items like ConnString, Equals, GetHashCode, GetType,
ReferenceEquals, ToString etc.

So isn't this a disadvantage of using user-defined interfaces? Because
making a reference to the interface IDBSettings from the ASPX page
means I won't be able to use the built-in functions like Equals,
GetHashCode, GetType etc. (which would be available if I make the
reference to the DBSettings class instead). If I want to do the same
task which the function, say, Equals does, then I have to add more
code within the interface & the class (in the class file) which in
turn means more work for the programmer. So is using user-defined
interfaces worthwhile?
It's of course only
useful when one method creates the object and another method uses the
object (in the shape of the interface)

Sorry, Goran, I couldn't exactly understand the above statement. Could
you please elaborate on it a little bit (maybe with an example as I
find it easier to understand things with examples instead of just a
textual explanation)?

Thanks once again for your co-operation.

Regards.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Finally I could make that code work. This is the class (.vb) code (I
am reproducing the entire code so that someone else can benefit from
it in the future; after all everyone won't be as fortunate as me to
have a helpful guide like you):

8< snip
Public Function QueryDB(ByVal qry As String) As SqlDataReader
Implements IDBSettings.QueryDB
sqlConn = New SqlConnection(ConnectionString)
sqlCmd = New SqlCommand(qry, sqlConn)

sqlConn.Open()
Return sqlCmd.ExecuteReader
sqlConn.Close()

You are still closing the connection. The data reader will not be able
to read any data from the database. It may work if the result is very
small so that it fits in the data buffer, but that's not a reasonable
limitation for a method like this.

You either have to specify CommandBehaviour.CloseConnection so that the
connection is closed when the data reader is closed, or supply another
method that can be used to close the connection after the data has been
read.
End Function
End Class

8< snip
There is one thing I noticed when I made the reference to the
IDBSettings interface (instead of the DBSettings class) in the ASPX
code. I use Visual Web Developer 2005 for ASP.NET apps. When I typed

boDBSettings.

(notice the dot after boDBSettings), then VWD's intellisense lists
only 2 items - the property named ConnectionString & the function
named QueryDB but if I make the reference to the DBSettings class,
then apart from ConnectionString & QueryDB, VWD's intellisense also
lists other items like ConnString, Equals, GetHashCode, GetType,
ReferenceEquals, ToString etc.

So isn't this a disadvantage of using user-defined interfaces? Because
making a reference to the interface IDBSettings from the ASPX page
means I won't be able to use the built-in functions like Equals,
GetHashCode, GetType etc. (which would be available if I make the
reference to the DBSettings class instead). If I want to do the same
task which the function, say, Equals does, then I have to add more
code within the interface & the class (in the class file) which in
turn means more work for the programmer. So is using user-defined
interfaces worthwhile?

As you are using the interface where you just as well could use the
class, you don't see the benefits of using an interface.
Sorry, Goran, I couldn't exactly understand the above statement. Could
you please elaborate on it a little bit (maybe with an example as I
find it easier to understand things with examples instead of just a
textual explanation)?

If you are creating the instance of the class in the same method as you
use it, it's rarely useful to use the interface at all. It's when you
create the instance in one method and sends it to another method that
there are any benefits.

This is used more often than most people know, for example if you create
a list from an array:

int[] values = new int[] { 1, 2, 3, 4 };
List<int> list = new List<int>(values);

(Sorry about the C# code, but that's what I usually use. Hope you can
follow it.)

The constructor of the List class doesn't take an array as argument, it
takes in IEnumerable. That way there only has to be one constructor that
creates a List from a collection of values, instead of one for Array,
one for List, one for Queue, one for Stack, one for SortedList, one for
Dictionary, one for LinkedList, one for SortedDictionary... and so on.

When you call the constructor for the List with an array, the reference
is not sent as a reference to an array, it's sent as a reference to
IEnumerable. By using the array in the call, you are implicitly casting
it into the interface before it's sent to the constructor.

The List constructor that uses the interface doesn't care if it's
actually an Array, a List, a Stack, a Queue... etc. that is implementing
the interface. It only cares about what the interface offers, i.e. that
it can loop through the items in the collection and get the values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top