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 / December 2007

Tip: Looking for answers? Try searching our database.

Interface called without implementing all methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 07 Dec 2007 05:26 GMT
I am a little confused here.

I was under the impression that before you can use an interface (and you
can't instantiate an interface), you have to implement all the methods.

But I am trying to set up a Generic DataBase class and this seems to work.
I am not instantiating the IDbConnection interface and am not defining any
of the methods (ChangeDatabase, Close, CreateCommand, Open).

In the following I am defining dbConn as an IDbConnection and am passing it
back as an IDbConnection type:
********************************************************************
Sub LoadGrid3()
 Dim dbConn As IDbConnection = _
      GetConnection("Persist Security Info=False;Data Source=AW;Initial
Catalog=Inter")
 Dim dbCmd As SqlCommand = New SqlCommand("GetUsers", dbConn)
 dbCmd.CommandType = CommandType.StoredProcedure

 dbCmd.Parameters.Add("@UserID",SqlDBType.Int).value = 1
 dbConn.Open()

 DataGrid1.DataSource = dbCmd.ExecuteReader
 DataGrid1.DataBind()
End Sub

Function GetConnection(ConnectString As String) As IDbConnection
 Dim cnn As New SqlConnection(ConnectString)
 return cnn
End Function
**************************************************************************

Why does this work????

Thanks,

Tom
Jacques - 07 Dec 2007 06:38 GMT
I'm not so sure about the first part of your post, but with regards to the
part about "Why does this work?"... It's because your method 'GetConnection'
is instantiating a SqlConnection object and passing that back to the caller.
In other words your function is not actually returning an IDbConnection, but
rather an instance of a SqlConnection object.

Hope it helps
Jacques
sloan - 07 Dec 2007 13:38 GMT
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!126.entry

it you take a look there, and look at the IAnimal / AnimalFactory example.
you can see what's going on
(as far as the design pattern goes)

Aka, you can write this code.

IAnimal ian = new Dog();

as long as Dog : IAnimal
(  :  =  implements of course  )

>I am a little confused here.
>
[quoted text clipped - 33 lines]
>
> Tom
Barrie Wilson - 07 Dec 2007 16:57 GMT
>I am a little confused here.
>
> I was under the impression that before you can use an interface (and you
> can't instantiate an interface), you have to implement all the methods.

you're not "calling an interface" as your Subject suggests; you're declaring
dbConn as being of type IDbConnection and you will get no complaint from the
compiler on this.  If, on the other hand, you create a new class and
implement IDbConnection

   class newClass : IDbConnection

you *must* implement all the members in the interface.

Importantly, notice that SqlConnection DOES implement all of the members of
interface IDbConnection.  Accordingly, when you pass dbConn around to a
method expecting an IDbConnection type, you're ok.

I'm not clear why you need to declare dbConn as type IDbConnection here but
perhaps you have reasons to do so.

> But I am trying to set up a Generic DataBase class and this seems to work.
> I am not instantiating the IDbConnection interface and am not defining any
[quoted text clipped - 28 lines]
>
> Tom

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.