DataRow.Item

G

GaryDean

The docs say that the DataRow class has an Item property so I could set a
value without having to know the column name i.e. myDR.Item[0] = myString;

but, there is no Item property in the DataRow.

What am I not understanding?
 
K

Karl Seguin [MVP]

It's a default property, which means you access it via indexes ([] in C#, ()
in VB.NET).

row[0] = myString;

Karl
 
W

Walter Wang [MSFT]

Hi Gary,

In addition to normal parameterless properties, .NET programming languages
also support parameterful properties, whose get accessor methods accept one
or more parameters and whose set accessor methods accept two or more
parameters. Different programming languages expose parameterful properties
in different ways. Also, languages use different terms to refer to
parameterful properties: C# calls them indexers and Visual Basic calls them
default properties.

In C#, parameterful properties (indexers) are exposed using an array-like
syntax. In other words, you can think of an indexer as a way for the C#
developer to overload the [] operator.

Use BitArray for example:

public sealed class BitArray {

public Boolean this[Int32 bitPos] {
get {
...
}
set {
...
}
}

...
}

It's quite common to create an indexer to look up values in an associative
array. Unlike parameterless properties, a type can offer multiple,
overloaded indexers as long as their signatures differ.

The CLR internally emit either two or three following items into the
resulting managed assembly:

* A method representing the parameterful property's get accessor method.
This is emitted only if you define a get accessor method for the property.
* A method representing the parameterful property's set accessor method.
This is emitted only if you define a set accessor method for the property.
* A property definition in the managed assembly's metadata, which is
always emitted.

For the BitArray class shown above, the compiler compiles the indexer as
though the original source code were written as follows:

public sealed class BitArray {

public Boolean get_Item(Int32 bitPos) { ... }

public void set_Item(Int32 bitPos, Boolean value) { ... }

...
}

The compiler automatically generates names for these methods by prepending
get_ and set_ to the indexer name. Because the C# syntax for an indexer
doesn't allow the developer to specify an indexer name, the C# compiler
team had to choose a default name to use for the accessor methods; they
choose Item.

When examining the .NET Framework Reference documentation, you can tell if
a type offers an indexer by looking for a property named Item.

Therefore, the answer to your question is you can just use row[0] to access
the property.

Hope this helps. Please feel free to post here if anything is unclear.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top