0% found this document useful (0 votes)
56 views2 pages

Weekday String Indexer in C#

This document contains code that defines a DayCollection class with methods to index and retrieve days of the week as integers. It initializes a DayCollection object, calls its indexer to get the integer for "Fri", and demonstrates an exception is thrown if an invalid day name is passed.

Uploaded by

Sudeep Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views2 pages

Weekday String Indexer in C#

This document contains code that defines a DayCollection class with methods to index and retrieve days of the week as integers. It initializes a DayCollection object, calls its indexer to get the integer for "Fri", and demonstrates an exception is thrown if an invalid day name is passed.

Uploaded by

Sudeep Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[INDEXER

EXAMPLE] 1

//Indexer Example

class DayCollection
{

string[] days = { "Sun", "Mon", "Tues", "Wed",
"Thurs", "Fri", "Sat" };
// This method finds the day or returns -1
private int GetDay(string testDay)
{


for (int j = 0; j < [Link]; j++)
{
if (days[j] == testDay)
{
return j;
}
}
throw new
[Link](testDay, "testDay
must be in the form \"Sun\", \"Mon\", etc");
}
// The get accessor returns an integer for a
given string
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
[Link](week["Fri"]);
// Raises ArgumentOutOfRangeException

[Link](week["Made-up
Day"]);
// Keep the console window open in debug
mode.
[Link]("Press any key to
exit.");
[Link]();
}
}

You might also like