DataRow and the protected internal constructor

Go To StackoverFlow.com

3

How can a DataTable create a new instance of a DataRow with the NewRow method if the constructor of the DataRow class is protected internal and DataTable doesn't inherit from DataRow?

Example:

class Program
{
    static void Main()
    {
        // error: inaccessible due to its protection level
        DataRow dr = new DataRow(); 

        // works
        DataRow dr = new DataTable().NewRow();
    }
}
2012-04-04 17:59
by Snake


2

protected internal means "accessible by derived classes" and "accessible by other classes in the same assembly". DataTable and DataRow are in the same assembly, so DataTable has access to all of DataRow's internal members.

2012-04-04 21:18
by NoName
protected internal means "accessible by derived classes in the same assembly", but DataTable is not derived from DataRow - Snake 2012-04-04 21:24
No, protected internal means what I said it means. See http://msdn.microsoft.com/en-us/library/ms173121(v=vs.100).asp - NoName 2012-04-04 21:27
My mistake, thank`s - Snake 2012-04-04 22:07
You're welcome, in both senses : - NoName 2012-04-04 22:11


0

Hope you already got an answer for this.

But still I am adding my answer to this to address “Why it is designed in this way”.

As “hvd” mentioned they are in same assembly that’s why DataTable able to create instance of DataRow.

The reason for this approach is:

• Data row contains values for each column • Ideally an array internally used to store these values

• So each data row contains array which contains values

• But data row will not be knowing the size of the array to initialize

• Which depends on number of columns in the data table

• But data table knows how many columns in the table

• That’s why it takes responsibility to create OR set the array size of the DataRow

2013-08-14 07:07
by CreativeManix
Ads