471,354 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

More then one array property?

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
Jan 17 '06 #1
8 1347
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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

Jan 17 '06 #2

"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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).
Jan 17 '06 #3
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.
--

Stoitcho Goutsev (100)

"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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

Jan 17 '06 #4

Daniel O'Connell [C# MVP] wrote:
"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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 :)

--
Larry Lard
Replies to group pleas

Jan 17 '06 #5
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

"Rainer Queck" <Ra****@noemail.noemail> schrieb im Newsbeitrag
news:eN**************@tk2msftngp13.phx.gbl...
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

Jan 17 '06 #6
Hi,
"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 17 '06 #7
Larry Lard <la*******@hotmail.com> wrote:

Daniel O'Connell [C# MVP] wrote:
"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eN**************@tk2msftngp13.phx.gbl...
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 17 '06 #8
I've found this interesting example:
http://www.codeproject.com/useritems/Indexers.asp

Maybe it helps.
Mayby it hepls.

Best Regards
Darek
Jan 17 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by point | last post: by
14 posts views Thread by Nikhil Patel | last post: by
3 posts views Thread by simonc | last post: by
5 posts views Thread by Sam | last post: by

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.