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;Initi al
Catalog=Inter")
Dim dbCmd As SqlCommand = New SqlCommand("Get Users", dbConn)
dbCmd.CommandTy pe = CommandType.Sto redProcedure
dbCmd.Parameter s.Add("@UserID" ,SqlDBType.Int) .value = 1
dbConn.Open()
DataGrid1.DataS ource = dbCmd.ExecuteRe ader
DataGrid1.DataB ind()
End Sub
Function GetConnection(C onnectString As String) As IDbConnection
Dim cnn As New SqlConnection(C onnectString)
return cnn
End Function
*************** *************** *************** *************** **************
Why does this work????
Thanks,
Tom 3 1049
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
http://sholliday.space s.live.com/Blog/cns!A68482B9628 A842A!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$******** ******@TK2MSFTN GP02.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;Initi al
Catalog=Inter")
Dim dbCmd As SqlCommand = New SqlCommand("Get Users", dbConn)
dbCmd.CommandTy pe = CommandType.Sto redProcedure
dbCmd.Parameter s.Add("@UserID" ,SqlDBType.Int) .value = 1
dbConn.Open()
DataGrid1.DataS ource = dbCmd.ExecuteRe ader
DataGrid1.DataB ind()
End Sub
Function GetConnection(C onnectString As String) As IDbConnection
Dim cnn As New SqlConnection(C onnectString)
return cnn
End Function
*************** *************** *************** *************** **************
Why does this work????
Thanks,
Tom
"tshad" <tf*@dslextreme .comwrote in message
news:e$******** ******@TK2MSFTN GP02.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;Initi al
Catalog=Inter")
Dim dbCmd As SqlCommand = New SqlCommand("Get Users", dbConn)
dbCmd.CommandTy pe = CommandType.Sto redProcedure
dbCmd.Parameter s.Add("@UserID" ,SqlDBType.Int) .value = 1
dbConn.Open()
DataGrid1.DataS ource = dbCmd.ExecuteRe ader
DataGrid1.DataB ind()
End Sub
Function GetConnection(C onnectString As String) As IDbConnection
Dim cnn As New SqlConnection(C onnectString)
return cnn
End Function
*************** *************** *************** *************** **************
Why does this work????
Thanks,
Tom
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
thanks to clear my stupid mind
regards
|
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
|
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 you are
implementing in the class.
Other than to resolve the problem that arises when a class
implements two interfaces with the same method signature,
what good is it?
|
by: Alex Sedow |
last post by:
Example 1
interface I
{
string ToString();
}
public class C : I
{
public void f()
{
|
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 MyPrivateMethod(); // I cant define any accessibility here, well
ok !
}
| |
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 be
implemented. So if you have a class which inherits base class that
inherts an interface, then your classes will have a standard. I suppose
you can also check for interface at run time say when dll is loaded and
see if it implememts whats...
|
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 by Sets.
- ICollection cannot decide containment
- IList promises indexability by the natural numbers, which is not
achievable (since i hash elements, not sort them).
- IDictionary is definatly not setlike.
Although I can, of course, define...
|
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 includes.
My question is, if you must declare each method (as opposed to simply
'using' those methods like an #included file), then why not just
declare the methods on your own as if you created them yourself? What
advantage does using an interface...
|
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 subroutines, etc. Now I'm
trying to study this new way and I'm getting some terms confused and can
find no clear definition (some even overlap or use two different words for
the same thing, even when they are actually different).
I'm reading a...
|
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 scenes in the foreach?
Also, I could not find where the interface signatures were for the
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |