| | |
Indexers
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
Can you understand this? http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
It seems a good explanation to me. If it ain't clear, please come back with your questions.
It seems a good explanation to me. If it ain't clear, please come back with your questions.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
Dear,
An indexer is a member of class that enables an object to be indexed in the same way as an array.
Consider the following code:
An indexer is a member of class that enables an object to be indexed in the same way as an array.
Consider the following code:
•
•
•
•
using System;
public class Date
{
int d, m, y;
// Constructors
public Date()
{
d = DateTime.Now.Day;
m = DateTime.Now.Month;
y = DateTime.Now.Year;
}
public Date(int d, int m, int y)
{
this.y = y;
this.m = m;
this.d = d;
}
// Properties
public int Day
{
get { return d; }
set { d = value; }
}
public int Month
{
get { return m; }
set { m = value; }
}
public int Year
{
get { return y; }
set { y = value; }
}
public string DMY
{
get
{
return d + "-" + m + "-" + y;
}
}
public string MDY
{
get
{
return m + "-" + d + "-" + y;
}
}
// Indexers
public int this[string i]
{
get
{
if (i == "day")
return d;
else
if (i == "month")
return m;
else
return y;
}
set
{
if (i == "day")
d = value;
if (i == "month")
m = value;
if (i == "year")
y = value;
}
}
public int this[int i]
{
get
{
if (i == 0)
return d;
else
if (i == 1)
return m;
else
return y;
}
set
{
if (i == 0)
d = value;
if (i == 1)
m = value;
if (i == 2)
y = value;
}
}
}
class MainApp
{
static void Main()
{
Date k = new Date();
Console.WriteLine(k.DMY);
Console.WriteLine(k.MDY);
k.Day = 8;
Console.WriteLine(k.DMY);
k[0] = 10;
k[1] = 3;
k[2] = 2010;
Console.WriteLine(k[0]);
k["month"] = 2;
Console.WriteLine(k["day"] + "-" + k["month"] + "-" + k[2]);
// Another example with string object
string name = "Hello";
Console.WriteLine(name[1]);
}
}
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: Saving Images into the database
- Next Thread: Using stored procecure in MYSQL
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox class client combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms ftp function gdi+ httpwebrequest image index input install label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






