473,385 Members | 1,355 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.

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

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

Aug 30 '06 #1
3 1919

<we*******@1stmiami.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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

Aug 30 '06 #2
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

Mythran wrote:
<we*******@1stmiami.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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
Aug 31 '06 #3
John Liu wrote:
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.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 31 '06 #4

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

Similar topics

4
by: J. Oliver | last post by:
I am attempting to copy a portion of a string into another string. Say I have "abcdef" in string 'a' and want to copy only "bcd" into a new string 'b'. The following function came to mind: ...
8
by: 2G | last post by:
Hi, Could anyone give me a hint how to generate this using codedom, I know how to generate properties and methods .... but this I can't seem to find :s. public int this { get { }
11
by: wASP | last post by:
Hi, I've got a pair of int properties in a class. The properties in question are indexing values - but that's not relevant to my problem - or it's just symptomatic ... sort of. They are...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
6
by: jim_geissman | last post by:
Can I create an index on a variation of a column that isn't actually in the table? I have a ParcelNumber column, with values like 123 AB-670 12345ABC 000-00-040 12-345-67 AP34567890
8
by: phillip.s.powell | last post by:
This query produces the following error: I'm sorry but I must have this "column" in the query, it's vital for required sorting order (you have to sort image_location_country in alphanumeric...
2
by: sklett | last post by:
I have the need to enter a hex value IE: 0xffff into a datagrid column that is backed by an int property for a custom object. in other words, I have a class like this: class MyEntity { private...
1
by: recherche | last post by:
Hola! I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain. Please help! ...
3
by: mdshafi01 | last post by:
Please can any one able to explain about this type of declaration. int _port;
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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.