public DataValue this[int column] indexing with int ???

W

webmaster

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();
}
}
 
M

Mythran

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.. :) ..

The this[] property is what is called an "indexer". Quoted from MSDN:

"Indexers permit instances of a class or struct to be indexed in the same
way as arrays. Indexers are similar to properties except that their
accessors take parameters."

ms-help://MS.MSDNQTR.2003FEB.1033/csref/html/vcerrUsingIndexers.htm

HTH :)

Mythran
 
J

John Liu

Some history, perhaps. I think the history of this goes back to VB6
(and may be other languages), there was this incredibly frustrating
idea called the default property. It got murdered in VB.NET (and there
was much rejoicing!), but a useful variant of it, the default indexer,
remained and was included in various other languages.

class MyCollection {
public string[100] Items;
}

means you can access the internal collection via:
MyCollection collection = new MyCollection();
collection.Items[0] = "hello";
collection.Items[1] = "world";

---

When you add a default indexer:

public string this[int index] {
get { return Items[index]; }
set { Items[index] = value; }
}

Then it allows you to do this:
MyCollection collection = new MyCollection();
collection[0] = "hello";
collection[1] = "world";

which is a slight improvement in the look of the syntax, without the
confusing factor of the default property back in VB6. Plus, it makes
your class look more like a proper collection instead of some wrapper.

Couple'd with encapsulation, you can have:

class MyCollection {
private string[100] Items; // make this private
public string this[int index] {
get { return Items[index]; } // provide only accessor
}
}

which essentially makes MyCollection a read-only collection.

jliu
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.. :) ..

The this[] property is what is called an "indexer". Quoted from MSDN:

"Indexers permit instances of a class or struct to be indexed in the same
way as arrays. Indexers are similar to properties except that their
accessors take parameters."

ms-help://MS.MSDNQTR.2003FEB.1033/csref/html/vcerrUsingIndexers.htm

HTH :)

Mythran
 
L

Larry Lard

John said:
Some history, perhaps. I think the history of this goes back to VB6
(and may be other languages)

VBA more than VB.
, there was this incredibly frustrating
idea called the default property. It got murdered in VB.NET (and there
was much rejoicing!)

No it didn't:

Public Class Foo

Private BarStuff As Integer() = New Integer(10) {}
Default Public Property Bar(ByVal i As Integer) As Integer
Get
Return BarStuff(i)
End Get
Set(ByVal value As Integer)
BarStuff(i) = value
End Set
End Property
End Class

And look, in VB.NET you can have default properties indexes by more than
one parameter! Can't do that with a C# indexer:

Public Class Fooz

Private BarStuff As Integer(,) = New Integer(10, 20) {}
Default Public Property Bar(ByVal i As Integer, ByVal j As Integer)
As Integer
Get
Return BarStuff(i, j)
End Get
Set(ByVal value As Integer)
BarStuff(i, j) = value
End Set
End Property
End Class

Usage:

Sub Main()

Dim f As New Foo, z As New Fooz

f(3) = 5

z(4, 5) = 6
End Sub


This concludes the cross-cultural lesson for today.
 

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