Solved

Why there is no New constructor for dataReader?

Posted on 2014-10-02
3
186 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
3 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 350 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 150 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

679 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