Pardon my not knowing where to find this one... but try finding the
meaning of "this" on the net or in reference books.
I'm trying to follow this example from Apress book - Introduction to
C# 2.0 (page 179) and it bothers me that I don't quite get something
I'm sure should be understood at this point of the book.
Line 53: public DataValue this[int column]
I think "this" means the local member.. Can somebody explain (in
Laymans terms) why "this" ? and what does [] say? this[int column] is
not an array right? Property DataValue is expecting an int right ??
why not :
public DataValue (int column)
Also, starting at line 59
set
{
row[column - 1] = value;
}
What is exactly is DataValue doing? the program does not appear to be
using the above set.
thanks .. maybe I need to go back to page 1.. :) ..
// 19 - Indexers and Enumerators\Indexing with an Integer Index
// copyright 2000 Eric Gunnerson
using System;
using System.Collections;
class DataValue
{
public DataValue(string name, object data)
{
this.name = name;
this.data = data;
}
public string Name
{
get
{
return (name);
}
set
{
name = value;
}
}
public object Data
{
get
{
return (data);
}
set
{
data = value;
}
}
string name;
object data;
}
class DataRow
{
public DataRow()
{
row = new ArrayList();
}
public void Load()
{
/* load code here */
row.Add(new DataValue("Id", 5551212));
row.Add(new DataValue("Name", "Fred"));
row.Add(new DataValue("Salary", 2355.23m));
}
// the indexer
public DataValue this[int column]
{
get
{
return ((DataValue)row[column - 1]);
}
set
{
row[column - 1] = value;
}
}
ArrayList row;
}
class Test
{
public static void Main()
{
DataRow rowx = new DataRow();
rowx.Load();
Console.WriteLine("Column 0: {0}", rowx[1].Data);
rowx[1].Data = 12; // set the ID
Console.WriteLine("Column 0: {0}", rowx[1].Data);
Console.Read();
}
}