How does the "this" keyword work in this context

P

Polaris431

I notice that the Dataset designer generates code for properties that
look like this:

public string ID {
get {
try {
return ((string)
(this[this.tablePersons.IDColumn]));
}
catch (System.InvalidCastException e) {
throw new System.Data.StrongTypingException("The
value for column \'ID\' in table \'Persons\' is DBNull.", e);
}
}
set {
this[this.tablePersons.IDColumn] = value;
}
}

In "set", the keyword "this" is used. This is the first time I've
notice it used like this. It seems to act like an indexer but where is
the data stored that it is referring to?
 
A

amdrit

this has an indexer on columns which can be accessed by a datacolumn instead
of an int for an index or a string for column name. this.tablepersons is
the parenttable that knows about all the datacolumns. Having the
tablepersonsdatarow access it's column data in this manner ensures that the
schema is enforced in the entire table, as this would cause a compile error
before execution if the datacolumn was not defined in the datatable.

this[] <-- accesses it's data via datacolumn

this.tablePersons.IDColumn <-- datacolumn defiened in the parent table
object to hold the ID for the current personrow.
 
P

Polaris431

You said:
this[] <-- accesses it's data via datacolumn

How do you know that it is the datacolumn? Is there some default
property? The class is inherited from a DataRow. Are you saying that
because of this inheritance, the base default property is the
datacolumn you are showing above?
 
M

mpetrotta

You said:
this[] <-- accesses it's data via datacolumn

How do you know that it is the datacolumn? Is there some default
property? The class is inherited from a DataRow. Are you saying that
because of this inheritance, the base default property is the
datacolumn you are showing above?

The System.Data.DataRow class has several (six, to be exact) overloads
of its indexer. One of them takes a DataColumn:

public object this[DataColumn column]
{
get
{
this.CheckColumn(column);
int defaultRecord = this.GetDefaultRecord();
return column[defaultRecord];
}
set
...
}
}

(I got this from Reflector, but MSDN also lists the indexers, under
the "Item" property)

Since your typed DataRow derives from System.Data.DataRow, you have
those indexers available to use.

Michael
 
P

Peter Duniho

[...]
(I got this from Reflector, but MSDN also lists the indexers, under
the "Item" property)

Since your typed DataRow derives from System.Data.DataRow, you have
those indexers available to use.

And just to be clear: the "this" keyword is isn't particular to the use
of the indexers. It's just that the syntax in that case requires _some_
identifier to apply the indexer syntax to (ie "[]").

The "this" keyword "works" in this context the same as it does in any
other context. It's just that it's basically required here, where in
most other situations it's not.

Pete
 
B

Ben Voigt [C++ MVP]

Peter Duniho said:
[...]
(I got this from Reflector, but MSDN also lists the indexers, under
the "Item" property)

Since your typed DataRow derives from System.Data.DataRow, you have
those indexers available to use.

And just to be clear: the "this" keyword is isn't particular to the use of
the indexers. It's just that the syntax in that case requires _some_
identifier to apply the indexer syntax to (ie "[]").

The "this" keyword "works" in this context the same as it does in any
other context. It's just that it's basically required here, where in most
other situations it's not.

To illustrate Peter's point:

DataRow r = this;
r[column]

works exactly the same as

this[column]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top