473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Interface called without implementing all methods

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).valu e = 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
Dec 7 '07 #1
3 1025
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

Dec 7 '07 #2
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 )


"tshad" <tf*@dslextreme.comwrote in message
news:e$**************@TK2MSFTNGP02.phx.gbl...
>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).valu e = 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


Dec 7 '07 #3

"tshad" <tf*@dslextreme.comwrote in message
news:e$**************@TK2MSFTNGP02.phx.gbl...
>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
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).valu e = 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


Dec 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serge Calderara | last post by:
Dear all, I read many documents and article based on Interface, but I am still blind with that and start to have no more hairs. I try to clearly understand when should I create Interface ? ...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
6
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
14
by: Laurent Vigne | last post by:
Hello, I would like to know how to make the methods of an interface inaccessible from outside Example: ---------------------------------------------- internal Interface ImyInterface { void...
9
by: phl | last post by:
hi, I am kind of confused aobut interfaces and abstract classes. In short as I understand it, an interface is like a contract between the class and the interface, so that certain funtions must...
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
13
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface...
6
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
4
by: jmDesktop | last post by:
In the code below from MSDN How do the PeopleEnum methods ever get called? foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); What is going on behind the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.