473,803 Members | 3,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\Ind exing with an Integer Index
// copyright 2000 Eric Gunnerson
using System;
using System.Collecti ons;
class DataValue
{
public DataValue(strin g 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("Sala ry", 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.WriteLi ne("Column 0: {0}", rowx[1].Data);
rowx[1].Data = 12; // set the ID
Console.WriteLi ne("Column 0: {0}", rowx[1].Data);
Console.Read();
}
}

Aug 30 '06 #1
3 1933

<we*******@1stm iami.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.2003 FEB.1033/csref/html/vcerrUsingIndex ers.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.Item s[0] = "hello";
collection.Item s[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*******@1stm iami.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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.2003 FEB.1033/csref/html/vcerrUsingIndex ers.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*******@googl email.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
11899
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: public string GetText(string a, int start, int end) { int i; string b;
8
5214
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
1377
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 declared as follows:
1
4239
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
6
2082
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
3289
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 order, however, that column can also be null, BUT all NON-NULL fields MUST BE FIRST before all NULL fields!) I'm not sure what's happening, please help!
2
4210
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 int _start = 0; public int Start {
1
2763
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! Code Snippet 1:
3
1651
by: mdshafi01 | last post by:
Please can any one able to explain about this type of declaration. int _port;
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.