Declare a property with index.

M

Mr. X.

Hello.
How can I declare a property with index?

public int myProp[int ind]
{
int x;
switch(ind)
{
case 1:
x = 3;
case 2:
x = 4;
default:
x = 999;
}
get {return x; }
}

What should I write instead of : public int myProp[int ind]?

Thanks :)
 
F

Felix Palmen

* Mr. X. said:
Hello.
How can I declare a property with index?

public int myProp[int ind]
{
int x;
switch(ind)
{
case 1:
x = 3;
case 2:
x = 4;
default:
x = 999;
}
get {return x; }
}

What should I write instead of : public int myProp[int ind]?

This is not a property (in fact, this isn't anything valid in C# syntax)
-- you have code outsite the getter/setter.

But apart from that, for an indexed property in C#, you have to create
a Type with a default indexer (with a property this[int index], best
make it implement IEnumerable) and use that type for a "regular"
property.

Regards,
Felix
 
F

Felix Palmen

* Peter Duniho said:
I have to disagree on the advice to "make it implement IEnumerable", at
least as a generalization like that. Whether something implements
IEnumerable or not has nothing to do with whether that type is used for
its indexer.

Well, the OP wanted to index using an integer. I can't think of a
sensible way to do this with something that is NOT enumerable.

Regards,
Felix
 
M

Mr. X.

Indeed, the object is not enumerable.

How can I search the whole Homeroom? (even not only by foreach statement).
I cannot do foreach, because I got the message :
foreach statement cannot operate on variables of type ... because ... does
not contain a public definition for 'GetEnumerator'

Thanks :)

Peter Duniho said:
Felix said:
Well, the OP wanted to index using an integer. I can't think of a
sensible way to do this with something that is NOT enumerable.

The type of the data used for the index isn't relevant. Not all integers
are mapped to enumerable data (*).

That's not even counting that we have no idea whether the actual use of an
indexer the OP has in mind is an integer. Sample code often has a way of
not being _precisely_ the way the code will be used in a real program
(never mind situations where the sample code isn't even valid, compilable
code :) ).

Pete


(*) Example:

In the code below, there are likely a handful of values that any given int
could map to. Perhaps a few dozen, for potentially thousands of integers.
It's not helpful at all to be able to enumerate the mapped-to values in
terms of the mapping itself.

class HomeroomIndexer
{
private Dictionary<int, Homeroom> _dict;

public Homeroom this[int studentID]
{
get
{
Homeroom homeroom;

if (_dict.TryGetValue(studentID, out homeroom))
{
return homeroom;
}

return null;
}
}
}

class StudentRoster
{
private HomeroomIndexer _hi;

public HomeroomIndexer Homeroom
{
get { return _hi; }
}
}

class A
{
private StudentRoster _roster;

void DoSomething(int studentID)
{
Homeroom homeroom = _roster.Homeroom[studentID];
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top