473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1420
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: point | last post by:
Hi there.. I have the folowing array => property = hexonet => property = 2003-12-01 18:46:20.0 => property = hexonet => property = 2003-12-02 02:59:15.0 => property = hexonet =>...
2
by: cody | last post by:
Does the clr allow more than one set and one get method for a property? Is it possible to use overloading for example set_Color(int c), set_Color(Color c)? from msdn: ...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
5
by: Jon Maz | last post by:
Hi All, I'm reasonably proficient with C#, and am starting with php5. I was happy to learn of the __get and __set methods, but am encountering a problem using them to set an object property of...
14
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
3
by: simonc | last post by:
Can you define a property as type Byte array of a specific length? I am trying to pass a byte array which is 3200 bytes in length from one form (in which the bytes are read from a file) to...
5
by: Sam | last post by:
Hi All I have couple of question regarding property of a class and structures. **** ---- Here is my class and structure ---- ***** 1. Public Structure MyPoint 2. Dim p As Point 3. ...
5
rahulephp
by: rahulephp | last post by:
Hi I need to print below array in the following manner. Output would be: Property Name: 1 Property Address: 1 Price: 1 Property Size: 1 URL 1
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.