Welcome Guest Search | Active Topics | Members | Log In | Register

Interface Options · View
rn5a@rediffmail.com
Posted: Wednesday, October 03, 2007 5:16:07 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 05:16:07
Date: Wed, 03 Oct 2007 12:16:07 -0700

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

rn5a@rediffmail.com
Posted: Wednesday, October 03, 2007 5:17:52 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 05:17:52
Date: Wed, 03 Oct 2007 12:17:52 -0700

On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
> 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?

bruce barker
Posted: Wednesday, October 03, 2007 6:06:57 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 06:06:57
Date: Wed, 03 Oct 2007 13:06:57 -0700

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)


rn5a@rediffmail.com wrote:
> On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
>> 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?
>
rn5a@rediffmail.com
Posted: Wednesday, October 03, 2007 7:06:11 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 07:06:11
Date: Wed, 03 Oct 2007 14:06:11 -0700

On Oct 3, 2:52 pm, G=F6ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> > 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 =3D New SqlConnection(ConnectionString)
> > sqlCmd =3D 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.
>
> --
> G=F6ran 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 =3D New SqlConnection(ConnectionString)
sqlCmd =3D 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 =3D 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#.

rn5a@rediffmail.com
Posted: Wednesday, October 03, 2007 7:08:14 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 07:08:14
Date: Wed, 03 Oct 2007 14:08:14 -0700

On Oct 3, 2:52 pm, G=F6ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> > 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 =3D New SqlConnection(ConnectionString)
> > sqlCmd =3D 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.
>
> --
> G=F6ran 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 =3D New SqlConnection(ConnectionString)
sqlCmd =3D 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 =3D 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#.

rn5a@rediffmail.com
Posted: Wednesday, October 03, 2007 7:14:41 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 07:14:41
Date: Wed, 03 Oct 2007 14:14:41 -0700

On Oct 3, 4:06 pm, r...@rediffmail.com wrote:
> On Oct 3, 2:52 pm, G=F6ran Andersson <gu...@guffa.com> wrote:
>
>
>
>
>
> > r...@rediffmail.com wrote:
> > > 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 =3D New SqlConnection(ConnectionString)
> > > sqlCmd =3D 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.
>
> > --
> > G=F6ran 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 =3D New SqlConnection(ConnectionString)
> sqlCmd =3D 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 =3D 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

Cowboy (Gregory A. Beamer)
Posted: Wednesday, October 03, 2007 10:27:43 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 10:27:43
Date: Wed, 3 Oct 2007 15:27:43 -0500

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.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
<rn5a@rediffmail.com> wrote in message
news:1191439072.489149.146910@g4g2000hsf.googlegroups.com...
> On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
>> 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?
>


rn5a@rediffmail.com
Posted: Wednesday, October 03, 2007 6:21:19 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 18:21:19
Date: Thu, 04 Oct 2007 01:21:19 -0700

On Oct 4, 1:19 am, G=F6ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> > 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 =3D New SqlConnection(ConnectionString)
> > sqlCmd =3D 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.)
>
> >> 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 tu=
rn
> >> means that you can write that code even before there is a class that
> >> implements the interface, and also that you can create different class=
es
> >> 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?
>
> 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 =3D New DBSettings
>
> Yes, you would. If the class implements the interface, you can do this:
>
> Dim dbs as IDBSettings
>
> dbs =3D 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.
>
> --
> G=F6ran 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 =3D 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 =3D 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.

Göran Andersson
Posted: Wednesday, October 03, 2007 11:52:28 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 23:52:28
Date: Wed, 03 Oct 2007 21:52:28 +0200

rn5a@rediffmail.com wrote:
> 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.

--
Göran Andersson
_____
http://www.guffa.com
Göran Andersson
Posted: Thursday, October 04, 2007 10:19:05 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 04/10/2007 10:19:05
Date: Thu, 04 Oct 2007 08:19:05 +0200

rn5a@rediffmail.com wrote:
> 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.)

>> 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?

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.

--
Göran Andersson
_____
http://www.guffa.com
Göran Andersson
Posted: Thursday, October 04, 2007 4:14:21 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 04/10/2007 16:14:21
Date: Thu, 04 Oct 2007 14:14:21 +0200

rn5a@rediffmail.com wrote:

>> (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.

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.

>> 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()

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).

>> 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 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.)

--
Göran Andersson
_____
http://www.guffa.com
rn5a@rediffmail.com
Posted: Friday, October 05, 2007 7:16:46 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 07:16:46
Date: Fri, 05 Oct 2007 14:16:46 -0700

On Oct 4, 7:14 am, G=F6ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> >> (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.
>
> 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.
>
> >> Dim dbs as IDBSettings
>
> >> dbs =3D 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 =3D 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).
>
> >> 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.So=
rt
> >> 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 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.)
>
> --
> G=F6ran Andersson
> _____http://www.guffa.com

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 =3D ConnString
End Get
Set(ByVal value As String)
ConnString =3D value
End Set
End Property

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

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

& this is the ASPX code:

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

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

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

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

sqlDReader.Close()
End If
End Sub
</script>
<form runat=3D"server">
<asp:DataGrid ID=3D"dg1" runat=3D"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.

Göran Andersson
Posted: Sunday, October 07, 2007 5:20:35 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 07/10/2007 05:20:35
Date: Sun, 07 Oct 2007 03:20:35 +0200

rn5a@rediffmail.com wrote:
> 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.

>> 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)?

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.

--
Göran Andersson
_____
http://www.guffa.com
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.277 seconds.