473,395 Members | 1,624 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,395 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 1421
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...
0
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
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...

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.