Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rn5a@rediffmail.com - 03 Oct 2007 20:16 GMT
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 - 03 Oct 2007 20:17 GMT
On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
> Suppose I have the following class code:
>
[quoted text clipped - 26 lines]
>
> Thanks

I mean what exactly is an interface as far as object-oriented
programming is concerned?
Peter Bromberg [C# MVP] - 03 Oct 2007 20:44 GMT
A good place to start:
http://msdn2.microsoft.com/en-us/library/87d83y5b(VS.80).aspx
Signature

Recursion: see Recursion
site:  http://www.eggheadcafe.com
unBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder:    http://www.blogmetafinder.com

> On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
> > Suppose I have the following class code:
[quoted text clipped - 30 lines]
> I mean what exactly is an interface as far as object-oriented
> programming is concerned?
bruce barker - 03 Oct 2007 21:06 GMT
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)

> On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
>> Suppose I have the following class code:
[quoted text clipped - 30 lines]
> I mean what exactly is an interface as far as object-oriented
> programming is concerned?
Cowboy (Gregory A. Beamer) - 03 Oct 2007 21:27 GMT
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.

Signature

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

*************************************************

| Think outside the box!

*************************************************
> On Oct 3, 2:16 pm, r...@rediffmail.com wrote:
>> Suppose I have the following class code:
[quoted text clipped - 30 lines]
> I mean what exactly is an interface as far as object-oriented
> programming is concerned?
Göran Andersson - 03 Oct 2007 20:52 GMT
> Suppose I have the following class code:
>
[quoted text clipped - 21 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.

Signature

Göran Andersson
_____
http://www.guffa.com

rn5a@rediffmail.com - 03 Oct 2007 22:06 GMT
On Oct 3, 2:52 pm, G?ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> > Suppose I have the following class code:
[quoted text clipped - 49 lines]
>
> - 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#.
rn5a@rediffmail.com - 03 Oct 2007 22:14 GMT
On Oct 3, 4:06 pm, r...@rediffmail.com wrote:
> On Oct 3, 2:52 pm, G?ran Andersson <gu...@guffa.com> wrote:
>
[quoted text clipped - 111 lines]
>
> - 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
rn5a@rediffmail.com - 03 Oct 2007 22:08 GMT
On Oct 3, 2:52 pm, G?ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> > Suppose I have the following class code:
[quoted text clipped - 49 lines]
>
> - 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#.
Göran Andersson - 04 Oct 2007 07:19 GMT
> 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
[quoted text clipped - 19 lines]
>     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
[quoted text clipped - 7 lines]
> 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
[quoted text clipped - 3 lines]
>
> 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.

Signature

Göran Andersson
_____
http://www.guffa.com

rn5a@rediffmail.com - 04 Oct 2007 09:21 GMT
On Oct 4, 1:19 am, G?ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> > Goran, you have said that an interface can have only the definitions
[quoted text clipped - 76 lines]
>
> - 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.
Göran Andersson - 04 Oct 2007 13:14 GMT
>> (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
>>
[quoted text clipped - 9 lines]
> 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
[quoted text clipped - 3 lines]
> 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.)

Signature

Göran Andersson
_____
http://www.guffa.com

rn5a@rediffmail.com - 05 Oct 2007 22:16 GMT
On Oct 4, 7:14 am, G?ran Andersson <gu...@guffa.com> wrote:
> r...@rediffmail.com wrote:
> >> (The method is unusable, though, as the database connection has to be
[quoted text clipped - 83 lines]
> G?ran 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 = 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.
Göran Andersson - 07 Oct 2007 02:20 GMT
> 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
[quoted text clipped - 4 lines]
>         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
[quoted text clipped - 18 lines]
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.