473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How does the "this" keyword work in this context

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?

Sep 25 '07 #1
5 1479
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.

"Polaris431" <jo*********@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
>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?

Sep 25 '07 #2
You wrote:
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?

Sep 25 '07 #3
On Sep 25, 7:09 am, Polaris431 <johannbl...@gmail.comwrote:
You wrote:
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

Sep 25 '07 #4
mp*******@gmail.com wrote:
[...]
(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
Sep 25 '07 #5

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
mp*******@gmail.com wrote:
>[...]
(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]
>
Pete

Sep 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Bruno | last post by:
I thought system("PAUSE"); was part of stdlib.h and cstdlib. Then why does this work? : #include <iostream> using namespace std; int main() { cout << "hello, world" << endl;...
44
by: Bamber | last post by:
Why does f get returned ? (compiled with gcc). #include<stdio.h> int func(int a); main() { int f; f=func(7); printf("f=%d",f);
5
by: cmay | last post by:
I was suprised to see that you can put a DataTable into viewstate on a page. Why does this work? I had understood that the DataTable was in serializable, which I thought was a requirement of...
9
by: william | last post by:
You guys are a great resource for learners such as I. I have seen the way that you go over and above in explaining even the most mudane things to beginners, and I think it is a great thing that...
3
by: Robert Smith | last post by:
Why/How does this work? I know that if I want to convert a signed byte (ie 0x7F) to unsigned number I can promote it to an integer, like this: byte a = (byte)0xAB; // -85 int b = a & 0xFF; //...
12
by: Gagan | last post by:
Hi All, I am new to VB.NET, and am confused by following code. The code basically obtains an instance of an object from a helper method. This helper method instantiates a new object, and returns...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.