?
Solved

Why there is no New constructor for dataReader?

Posted on 2014-10-02
3
Medium Priority
?
208 Views
Last Modified: 2014-10-05
This is a part of code I have:

SqlDataReader dr = cmd.ExecuteReader();

If dr is an object and SqlDataReader is a class? What the above code is doing?

Is it instantiating? Or, it already has been instantiated so we are just initializing it.

I appreciate for posing your comment explaining how this is working?
0
Comment
Question by:Mike Eghtebas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 1400 total points
ID: 40358378
There is one, every class has a constructor.

But if you look at the documentation for the basic DBDataReader (not a more specific one such as the SqlDataReader or the OleDBDataReader), it is defined as protected instead of public as most constructors are.

That means that only classes that inherits from the DataReader can call it. So it is called by the SqlDataReader (or any of the other data readers) because this one inherits from it, but you can't.

On their side classes such as the SqlDataReader have alternate constructors that are declared Friend (in VB) / internal (in C#). These types of methods can be called only internally inside of a project, inside of the System.Data.dll for the SqlDataReader.

When you call ExecuteReader, because that method is in the same dll, it can call the internal SqlDataReader, which in turn can call the protected DBDataReader that offers the base functionalities of a Data Reader. It passes back the created DataReader back to you as the return value of the ExecuteReader method.

I have not seen documentation from Microsoft that states why they designed the data reader mechanism that way, but this is often done for security reasons. I have seen such a mechanism used in a situation where a few things had to be set up before the class could be instantiated. Instead of requiring the user to write a lot of stuff every time he needed to create a new object (which goes against the ideas of object oriented programming), the programmer did something similar what Microsoft did with the data reader: call a method that will both do the proper initialization, instantiate the object and return it to you.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40358410
Thanks,

Mike
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 600 total points
ID: 40358918
These types of methods can be called only internally inside of a project, inside of the System.Data.dll for the SqlDataReader.
That is, unless InternalsVisibleToAttribute is used to expose internal members to specific assemblies--which, of course, System.Data would not be exposing anything to your code in this fashion!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question