Indexers

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 17
Reputation: Egypt Pharaoh is an unknown quantity at this point 
Solved Threads: 0
Egypt Pharaoh Egypt Pharaoh is offline Offline
Newbie Poster

Indexers

 
0
  #1
May 3rd, 2009
I don't understand the indexers Can any one explain it to me
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,941
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 279
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Indexers

 
0
  #2
May 3rd, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,613
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 469
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Indexers

 
0
  #3
May 4th, 2009
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:
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC