Hello all,
I am relatively new to C#, and I have a question about the class DataColumnCollection. How does one reference an indiviual column. It was my assumption that this would just be an array of DataColumns, but I have having problems with just pulling out one column. The only way I know how to do it now, is to do a foreach statement. For example, I want to reference DataColumnCollection(index).columnname, is that possible thank you!

DataColumnCollection
tdumitr
rimz
I will provide you a generalized answer so you can understand it when you need it later. Objects that represent themselves as arrays (such as collections) use subscripts to access members. In C++/C# you use the [] operator to access array elements (it's () in VB). So...
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for(int nIdx = 0; nIdx < 10; ++nIdx)
Console.WriteLine(arr[nIdx]);
You can tell which objects support "array"-style referencing by looking at the properties. In C# object's that support array referencing will have one or more properties named "this". Do not confuse a "this" property with the this keyword. The this keyword is used inside a class to reference the specific instance. The "this" property is used to define a property that permits the user to use array-style referencing (the braces) against the object.
class MyClass
{
private int m_Value;
public void Foo ( )
{
this.m_Value = 10; //this represents the instance
}
//This property permits a user to use array referencing with an integer
public int this [ int index ]
{
get { return m_Value * index; }
}
}
//Later in code
MyClass cls = new MyClass();
int value = cls[4];
Note that the "this" property can use parameters other than ints. Dictionaries use strings or other types as keys. You can have multiple "this" properties as well. Refer to Dictionary for an example.
A warning is needed here. The "this" property is a C# thing. When you look at documentation and even the real underlying code you'll see that the property is actually called Item. However in C# you'll never reference the real property name directly. I'm not even sure it would compile if you tried.
Now to your original question of DataColumnCollection. IList (of which almost all collections implement) contains a "this" property for integer indexing. ICollection (the other interface commonly implemented by collections) does not. Alas DataColumnCollection only implements ICollection. However it does implement the Item property (through the "this" property) and therefore supports referencing columns either by name or by index. If you look at the documentation for the class you'll see both overrides. Therefore array-style indexing works.
DataColumnCollection coll = ...;
coll.Add("Column1");
coll.Add("Column2");
coll.Add("Column3");
for (int nIdx = 0; nIdx < coll.Count; ++nIdx)
{
DataColumn col = coll[nIdx];
...
};
Or,
DataColumnCollection coll = ...;
coll.Add("Column1");
coll.Add("Column2");
coll.Add("Column3");
DataColumn col = coll["Column2"];
Hope this helps,
Michael Taylor - 5/25/06