Solved

Why there is no New constructor for dataReader?

Posted on 2014-10-02
3
166 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 33

Author Comment

by:Mike Eghtebas
ID: 40358410
Thanks,

Mike
0
 
LVL 74

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Image(2) 3 26
C# Error - Add Failed 12 49
Create XML 5 33
Can Amazon AppStream 2.0 be used for VB.NET and VB6 applications? 2 29
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now