More then one array property?

R

Rainer Queck

Hi NG,

how can I implement more then one array property to a class?

I have read about the Indexer, but as far as I can see this would only work
for a Collection type of class like list or arraylist.... an I could only
have one indexer per class right?

Coming from Delphi I know array properties like

property MyBoolArray[Index: Integer]: Boolean read GetMyBoolArray write
SetMyBoolArray;

Is there a other way to get array properties, which I have just not found
yet?

Thanks for help and hints.

Rainer
 
N

Nicholas Paldino [.NET/C# MVP]

Rainer,

You can always expose properties which expose class instances which have
indexers. Then, you can just use the indexer with the property name, like
so:

private List<string> myList;

public List<string> MyProperty
{
get
{
return myList;
}
}

// Get the list.
string myString = myObject.MyProperty[5];

Hope this helps.
 
D

Daniel O'Connell [C# MVP]

Rainer Queck said:
Hi NG,

how can I implement more then one array property to a class?

I have read about the Indexer, but as far as I can see this would only
work for a Collection type of class like list or arraylist.... an I could
only have one indexer per class right?

Coming from Delphi I know array properties like

property MyBoolArray[Index: Integer]: Boolean read GetMyBoolArray write
SetMyBoolArray;

Is there a other way to get array properties, which I have just not found
yet?

C#, sometimes sadly and sometimes fortunatly, doesn't support this, although
the runtime itself does. The primary workaround is to just write a class
with an indexer and use that as your property's type. It gives you the
client side syntax, atleast.

In your case, an array or collection seems sensible anyway. What would your
GetMyBoolArray\SetMyBoolArray do that the class MyBoolArray's indexer
wouldn't? It is a nice feature at times(for example giving different indexed
views of one piece of data) but one that sometimes leads to classes that do
to much(such as implementing your own bit array within an unrelated class).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Rainer Queck said:
Hi NG,

how can I implement more then one array property to a class?

I have read about the Indexer, but as far as I can see this would only
work for a Collection type of class like list or arraylist.... an I could
only have one indexer per class right?

You can have one indexer per class, how you implement it depends of you. You
are completely free to implement it as you need., the items you index refer
to can be stored anywhere/anyhow
;
Is there a other way to get array properties, which I have just not found
yet?

Nop, either you use a method or an internal class, I prefer the method
though.
 
S

Stoitcho Goutsev \(100\)

Rainer,

You got it right. There is one indexer per class. The indexer can have as
many overloads as you want, but they all have to differ in the signature. If
you want more than one array-like property you need to declare new class
that has indexer and return a object of this class.
You can declare this class nested in the main class declaration this way the
objects of this new "array-like" class will have access to all private
members of the outer class as long as it has reference to the outer object.
I know it sounds like a lot of work, but withe the new generics can come
real handy here.
 
R

Rainer Queck

Hi Nicholas, Daniel and Stoitcho,

thanks for your infos, hints and comments.

I didn't think of the possibility to expose indexed generic classes. That
sounds like a good way to handle the "array properties".

Thanks for your help!

Rainer
 
L

Larry Lard

Daniel said:
Rainer Queck said:
Hi NG,

how can I implement more then one array property to a class?

I have read about the Indexer, but as far as I can see this would only
work for a Collection type of class like list or arraylist.... an I could
only have one indexer per class right?

Coming from Delphi I know array properties like

property MyBoolArray[Index: Integer]: Boolean read GetMyBoolArray write
SetMyBoolArray;

Is there a other way to get array properties, which I have just not found
yet?

C#, sometimes sadly and sometimes fortunatly, doesn't support this, although
the runtime itself does.

VB.NET supports this as well. In fact in VB.NET, properties can have
arbitrary numbers of parameters, eg:

Public Property MyBoolArray(ByVal Index As Integer) As Boolean

Public Property My2DBoolArray(ByVal X As Integer, ByVal y As
Integer) As Boolean

even variable numbers of parameters:

Public Property SumProduct(ByVal ParamArray x() As Integer) As
Integer

and also, the indexer (the default property) can have a name:

Default Public Property Item(ByVal Index As Integer) As Object

so you can say myobject(1) or myobject.Item(1) as the fancy takes you.

But you're using C#, so these wonders are denied you :)
 
J

Jon Skeet [C# MVP]

Larry Lard said:
Rainer Queck said:
Hi NG,

how can I implement more then one array property to a class?

I have read about the Indexer, but as far as I can see this would only
work for a Collection type of class like list or arraylist.... an I could
only have one indexer per class right?

Coming from Delphi I know array properties like

property MyBoolArray[Index: Integer]: Boolean read GetMyBoolArray write
SetMyBoolArray;

Is there a other way to get array properties, which I have just not found
yet?

C#, sometimes sadly and sometimes fortunatly, doesn't support this, although
the runtime itself does.

VB.NET supports this as well. In fact in VB.NET, properties can have
arbitrary numbers of parameters, eg:

Public Property MyBoolArray(ByVal Index As Integer) As Boolean

Public Property My2DBoolArray(ByVal X As Integer, ByVal y As
Integer) As Boolean

even variable numbers of parameters:

Public Property SumProduct(ByVal ParamArray x() As Integer) As
Integer

and also, the indexer (the default property) can have a name:

Default Public Property Item(ByVal Index As Integer) As Object

so you can say myobject(1) or myobject.Item(1) as the fancy takes you.

But you're using C#, so these wonders are denied you :)

Actually, very few of them are denied you:

1) You can have indexers with as many properties you want, including
parameter arrays.

2) You can specify the name given to the indexer, using the
DefaultMemberAttribute

You just can't have multiple, differently-named indexers nor can you
refer to an indexer by name. It's definitely a pain, but it's not quite
as bad as you make out.
 

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