473,491 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

indexer question

Hi all,

I found this example in the help and thought this is great. But I can not
figure out how to
iterate thru the array and print out the values in the array.
Do I use a foreach (I tried could not get that to work) or a for loop (same
result)

my test harness below works so I know I am getting values into the grid,
which is a two
dimensioned array with an indexer[char,int]
a little code example please
thanks
grs

public static void Main()
{
Grid aGrid = new Grid();
aGrid['A',1] = 1;
Console.WriteLine(aGrid['A',1].ToString());

*** so here how do I loop thru the array

=======================
class Grid
{
const int NumRows = 26;
const int NumCols = 10;
int[,] cells = new int[NumRows, NumCols];
public int this[char c, int col]
{
get
{
c = Char.ToUpper(c);
if (c < 'A' || c > 'Z')
{
throw new ArgumentException();
}
if (col < 0 || col >= NumCols)
{
throw new IndexOutOfRangeException();
}
return cells[c - 'A', col];
}
set
{
c = Char.ToUpper(c);
if (c < 'A' || c > 'Z')
{
throw new ArgumentException();
}
if (col < 0 || col >= NumCols)
{
throw new IndexOutOfRangeException();
}
cells[c - 'A', col] = value;
}
}
}
Nov 15 '05 #1
2 1113
Got it, is this the standard way or is there another.
george

for (char aChar = 'A'; aChar < 'Z'; aChar++)
for (int i = 0; i < 10; i++)
Console.WriteLine(aGrid[aChar,i]);

"george r smith" <gs****@budgetext.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
Hi all,

I found this example in the help and thought this is great. But I can not
figure out how to
iterate thru the array and print out the values in the array.
Do I use a foreach (I tried could not get that to work) or a for loop (same result)

my test harness below works so I know I am getting values into the grid,
which is a two
dimensioned array with an indexer[char,int]
a little code example please
thanks
grs

public static void Main()
{
Grid aGrid = new Grid();
aGrid['A',1] = 1;
Console.WriteLine(aGrid['A',1].ToString());

*** so here how do I loop thru the array

=======================
class Grid
{
const int NumRows = 26;
const int NumCols = 10;
int[,] cells = new int[NumRows, NumCols];
public int this[char c, int col]
{
get
{
c = Char.ToUpper(c);
if (c < 'A' || c > 'Z')
{
throw new ArgumentException();
}
if (col < 0 || col >= NumCols)
{
throw new IndexOutOfRangeException();
}
return cells[c - 'A', col];
}
set
{
c = Char.ToUpper(c);
if (c < 'A' || c > 'Z')
{
throw new ArgumentException();
}
if (col < 0 || col >= NumCols)
{
throw new IndexOutOfRangeException();
}
cells[c - 'A', col] = value;
}
}
}

Nov 15 '05 #2

Hi George,

Thanks for posting in this group.
Foreach statement can be used to loop through arrays or collections.
For example, in Grid class, you can expose a print method, which will use
foreach to loop through cells array. Like this:
public void print()
{
foreach(int obj in cells)
{
Console.WriteLine(obj.ToString());
}
}

Also, you can treat Grid class as a collection, but you must implement
GetEnumerator method.
For more information to use foreach to loop through collection , please
refer to:
http://msdn.microsoft.com/library/de...us/csref/html/
vclrfusingforeachwithcollections.asp

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8331
by: Nikhil Patel | last post by:
Hi all, Is it possible to have more than one indexer in a class. If not, are there any good alternative ways to achieve similar functionality. Thanks... -Nikhil
4
1685
by: Dotnetjunky | last post by:
Hi, I'm really get confused on this issue. Hope that someone can help me get clear after this post. Take the class System.Web.Caching.Cache for example. In the documentation, there's a...
3
2523
by: george r smith | last post by:
I am converting a delphi chess program to C#, this is how I study a new language and I have the following problem. How do you use an enumeration value as an indexer for an array ? This is what I...
2
1401
by: Jerry Negrelli | last post by:
I'm trying to define a custom class indexer to take the place of an underlying Hashtable member so that values can be referred to as Class1 instead of Class1.someVar. The following code does...
7
485
by: Oren | last post by:
hi I have created a class with a simple indexer what is the syntax for adding the function "Remove" ... myClass.colors=Color.Black; // WORKS O myClass.colors.Remove(); // ?? .......
7
4607
by: Steph | last post by:
I'm learning C#. I need to implement an indexer using an array list. I have the following code and I'm getting an error "Inconsistent accessibility: indexer return type CRegs is less accessible...
5
1847
by: SpotNet | last post by:
Hello NewsGroup, I have a custom class and a collection for that custom class that inherits CollectionBase. As such; public class MyClass { private string datamember1 = string.Empty,...
3
1469
by: Nash Alexx | last post by:
Hello! I am quite new to C#, and one concept that really gives me a headache is "indexer". I have gone through the MSDN examples, and, at some level know how to use indexers. But, the thing is I...
17
2051
by: SemSem | last post by:
i want to know waht is an index and how we use it with a simple example including the main of the program . thanx -- Islam Khalil,
2
1962
by: hufaunder | last post by:
SITUATION: I have a class "MainClass" that has an indexer "SubClass1Indexer". This indexer returns an instance that implements a particular interface "ISubClass1". This interface defines another...
0
7118
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,...
0
7192
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4886
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...
0
4579
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1397
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
282
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...

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.