468,116 Members | 2,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,116 developers. It's quick & easy.

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 2047
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Abhishek Srivastava | last post: by
4 posts views Thread by DotNetJunkies User | last post: by
1 post views Thread by Marja Ribbers-de Vroed | last post: by
2 posts views Thread by Aamir Ghanchi | last post: by
4 posts views Thread by dascandy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.