| | |
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]);
}
}
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
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 |
.net access activedirectory algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox connect control conversion cryptographyc#winformsencryption csharp custom database datagrid datagridview dataset datetime degrees development disabled displayingopenforms draganddrop drawing encryption enum event excel file foreach form format forms ftp function gdi+ httpwebrequest image index index-error input install java label list listbox listview mandelbrot math mathematics mouseclick mysql operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox security server setup sleep socket sql statistics stream string table text textbox thread time timer totaldays update user usercontrol validation visual visualstudio webbrowser windows winforms wpf xml






