Question on DataReader Class
-
Hello, This is my first message here, so my Hello to you all again!!:) This question is not quite related to coding, not directly atleast. I am just curious to know how DataReader class in .Net works?? Scenario is: We do not call the constructor of DataReader class, this means that its constructor is marked "Private". Rather we use a Method, "ExecuteReader" of another Class "Command" while creating DataReader's object. I am trying to mimic this thing. I can do this easily if ExecuteReader method belongs to the same class i.e. DataReader, but I have no idea how to implement the same technique using 2 seperate classes. Command.ExecuteReader method returns a DataReader object, but does it also calls its constructor? Inheriting DataReader Class is not possible because it is also marked "Sealed", so how can I implement the same thing?? Thank you. Hope I have put the question well!! :rolleyes: --- HARSH GUPTA
-
Hello, This is my first message here, so my Hello to you all again!!:) This question is not quite related to coding, not directly atleast. I am just curious to know how DataReader class in .Net works?? Scenario is: We do not call the constructor of DataReader class, this means that its constructor is marked "Private". Rather we use a Method, "ExecuteReader" of another Class "Command" while creating DataReader's object. I am trying to mimic this thing. I can do this easily if ExecuteReader method belongs to the same class i.e. DataReader, but I have no idea how to implement the same technique using 2 seperate classes. Command.ExecuteReader method returns a DataReader object, but does it also calls its constructor? Inheriting DataReader Class is not possible because it is also marked "Sealed", so how can I implement the same thing?? Thank you. Hope I have put the question well!! :rolleyes: --- HARSH GUPTA
-
You can make the constructor
internal
, that means that any class in the same assembly can use it.--- b { font-weight: normal; }
If I make it
internal
, then I could even call it's constructor from Main method(in C#) or New Sub (in VB), which I am trying to avoid!! -
If I make it
internal
, then I could even call it's constructor from Main method(in C#) or New Sub (in VB), which I am trying to avoid!!Hi, it all depends on how you dstribute your classes onto your assemblies. If your main method is within the same assembly than it could surel create an instance. You should think if it would be better to separate your main method from other classes. Fact is that the different IDataReader implementations in the .Net framework all have internal constructors. Thus any class in the System.Data assembly could instantiate them. Robert
-
If I make it
internal
, then I could even call it's constructor from Main method(in C#) or New Sub (in VB), which I am trying to avoid!! -
Hi, it all depends on how you dstribute your classes onto your assemblies. If your main method is within the same assembly than it could surel create an instance. You should think if it would be better to separate your main method from other classes. Fact is that the different IDataReader implementations in the .Net framework all have internal constructors. Thus any class in the System.Data assembly could instantiate them. Robert
Thank you Robert and Guffa. So Assembly is the key in this scenario. Got it working. --- Harsh Gupta