473,326 Members | 2,099 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,326 software developers and data experts.

Instantiating from an Interface?

I thought interfaces have no implementation so how can I create a DataReader
from an IDataReader Interface and use it similarly to how I would use an
OleDBDataReader? How is it that the block of code below actually functions?
Does IDataReader have an implementation or not? Thanks in advance.

System.Data.IDataReader DR= db.ExecuteReader(dbCommandWrapper);
while(DR.Read())
{
Console.WriteLine(DR.GetValue(0).ToString() );
}

Nov 17 '05 #1
5 2187
Hi Scott,

The ExecuteReader call creates an instance which implements the
IDataReader interface.

Succes,
Bart

Scott Johnson wrote:
I thought interfaces have no implementation so how can I create a DataReader
from an IDataReader Interface and use it similarly to how I would use an
OleDBDataReader? How is it that the block of code below actually functions?
Does IDataReader have an implementation or not? Thanks in advance.

System.Data.IDataReader DR= db.ExecuteReader(dbCommandWrapper);
while(DR.Read())
{
Console.WriteLine(DR.GetValue(0).ToString() );
}


Nov 17 '05 #2
Hi Scott,

The ExecuteReader call creates an instance which implements the
IDataReader interface.

Succes,
Bart

Scott Johnson wrote:
I thought interfaces have no implementation so how can I create a DataReader
from an IDataReader Interface and use it similarly to how I would use an
OleDBDataReader? How is it that the block of code below actually functions?
Does IDataReader have an implementation or not? Thanks in advance.

System.Data.IDataReader DR= db.ExecuteReader(dbCommandWrapper);
while(DR.Read())
{
Console.WriteLine(DR.GetValue(0).ToString() );
}


Nov 17 '05 #3
On Fri, 21 Oct 2005 20:15:16 -0500, Scott Johnson wrote:
I thought interfaces have no implementation so how can I create a DataReader
from an IDataReader Interface and use it similarly to how I would use an
OleDBDataReader? How is it that the block of code below actually functions?
Does IDataReader have an implementation or not? Thanks in advance.

System.Data.IDataReader DR= db.ExecuteReader(dbCommandWrapper);
while(DR.Read())
{
Console.WriteLine(DR.GetValue(0).ToString() );
}


The return from ExecuteReader will be an object that implements the
IDataReader interface. In the case of an OleDbCommand object, it returns
an OleDbDataReader which implements IDataReader along with IDataRecord and
other interfaces.
--
Tom Porterfield
Nov 17 '05 #4
What might be tripping you up is that it's a little confusing that
interfaces are also types, even though they don't describe an entire
type as such. They are more of a partial type, or even more accurately,
a template specification for a partial type. Or more accurately yet, a
description of part of the interface of a type.

What you're actually getting back from that call to ExecuteReader() is
some instance of a class that, amongst other things, implements the
IDataReader interface. But you're viewing that instance through the
"lens" of the interface. Using the interface "type" lets you work with
any object that implements the interface, but you can only "see" and
work with the portion of the object that implements the interface. Even
though the complete instance with all its data and behaviors is in
memory, you can't get to all of it.

Really, when you think about it, interfaces are a way to partially get
around the restrictions of strong typing when the exact full type of an
object isn't known at design time. Unlike .NET, loosely typed,
late-bound environments have less need to support interfaces because the
runtime cares more about what the object *does* than what it *is*. So
if you call Foo() on some object in a loosely typed, late-bound
environment, all that matters is that the object has a Foo() method, and
you'll only get an error if the object lacks the method. Whereas in
..NET, everything is wired up at compile time. Interfaces are a way to
assure the compiler that while the object's type can't be known to the
compiler, it is at least known to implement Foo().

--Bob

Scott Johnson wrote:
I thought interfaces have no implementation so how can I create a DataReader
from an IDataReader Interface and use it similarly to how I would use an
OleDBDataReader? How is it that the block of code below actually functions?
Does IDataReader have an implementation or not? Thanks in advance.

System.Data.IDataReader DR= db.ExecuteReader(dbCommandWrapper);
while(DR.Read())
{
Console.WriteLine(DR.GetValue(0).ToString() );
}


Nov 17 '05 #5
MUS
Hello Scott!

What actually is going on is that "ExecuteReader()" method returns an
object which implements "IDataReader" interface. This is basic of
polymorphism.

Onething to keep in mind that, if the object that ExecuteReader()
returns has some methods of its own then they wont be available with
IDataReader type-instance, since those operations are specialized to
the object that ExecuteReader() returns

For the sake of example lets conside the following code:

1) We have a TaskManager class which has a public method DoAction which
returns an instance of IAction (just like ExecuteReader returns an
instance of IDataReader)

public class TaskManager
{
public IAction DoAction()
{
// Do something here and return one of the 2 IAction
implementation
// i.e. either returns a reference of InsertAction class
// or that of UpdateAction class
// return new InsertAction();
// return new UpdateAction();
}
}

2) IAction interface

public interface IAction
{
int Execute();
}

3) IAction interface implementations

public class InsertAction : IAction
{
public int Execute()
{
// Insert something in database and return
}

// Specialized method
public void InsertTest()
{
// Do something
}
}

public class UpdateAction : IAction
{
public int Execute()
{
// Update something in database and return
}

// Specialized method
public void UpdateTest()
{
// Do something
}
}

4) Client Code:

public class MainClass
{
public static void Main()
{
TaskManager taskMan = new TaskManager();
// This is the key point -- so pay attention
IAction action = taskManager.DoAction();
Console.WriteLine(action.ExecuteAction());
}
}

Note: After casting to IAction we have lost the identity that it is an
"InsertAction" or an "UpdateAction" so their specialized
operation/method [InsertTest()/UpdateTest()] cant be called.

Hope this might be of some help. Let me know in case of any
inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd

Nov 17 '05 #6

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

Similar topics

5
by: Abhishek Srivastava | last post by:
Hello All, I want my clients to use my remoted object by means of interfaces. These interfaces also ensure clean seperation between client and server. The approach I know to instantiate a CAO...
4
by: DotNetJunkies User | last post by:
Hi, Does anyone know how/if you can instantiate a C# reference type object dynamically? More specifically, my project has a number of classes that I've created and in some cases it would be very...
2
by: HarishP | last post by:
Hi, How to avoid instantiating the class more than 10 times Harish.P Sr. Software Engineer Comat Technologies Pvt. Ltd., Bangalore Email: harish.p@comat.com
1
by: Marja Ribbers-de Vroed | last post by:
I've been provided with a custom ActiveX DLL (written in C++) that reads a certiifcate to generate a signature for a passed XML string, and I'm having trouble instantiating it. The DLL is registered...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
3
by: kj7ny | last post by:
Is there a good example on instantiating, calling, using, etc., an API from Python? I need to use Python to talk to another system through that system's API's on Windows XP. Can you point me...
2
by: Aamir Ghanchi | last post by:
Hi, would I refer the aspx page from the class which has been instantiated in that aspx page. say I have Default.aspx. In the code page Default.aspx.cs I instantiate a class as MyClass mc =...
4
by: dascandy | last post by:
Hi, For a project I'm working on I'm kind-of-hacking my way around deriving a class from an interface or such to create a mock, but instead creating the mock directly. It is usable as the...
35
by: Rick Giuly | last post by:
Hello All, Why is python designed so that b and c (according to code below) actually share the same list object? It seems more natural to me that each object would be created with a new list...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.