Archived

This topic is now archived and is closed to further replies.

Traversing an array of unknown dimensions

This topic is 4949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, guys. I''m having some problems. I''m using C#. I''m passing an object to a method. The object is an array of type IRecordable (an interface I made), but with unknown dimensions (I don''t know how many []''s there will be after the name). I made a multiplicity() method to count the number of dimensions in the object. However, I can''t figure out how to traverse every element in the array without knowing its dimensions in advance. Please help. Thanks in advance.

Share this post


Link to post
Share on other sites
I''m not sure you understand my question (or maybe I just don''t understand your answer). What I''m saying is, the method could receive as an argument IRecordable, IRecordable[], IRecordable[][][][], etc. I have to be able to traverse all the elements regardless of the argument.

Share this post


Link to post
Share on other sites
All arrays are derived from the Array type, so you can make use of all those inherited methods. The Rank property tells you how many dimensions a passed array has. However, if you just want to traverse every element in the array, use foreach (enumerators basically). It will make sure to cover all the elements, even in a multidimensional array. It's a little more complex if you need to know the specific row/column of an element as you iterate, but not that much harder. This is the Array class reference - a very useful read

[edited by - Zipster on May 28, 2004 11:28:24 PM]

Share this post


Link to post
Share on other sites
Thanks, that''s EXACTLY what I was looking for! I didn''t realize it was that easy, I thought I''d have to implement all that functionality myself!

Share this post


Link to post
Share on other sites
One more question: is there a method/property that returns the type of the array? For example, Weapon[][] should return Weapon. I have a method that does this by getting the string representation of the array and stripping the []''s off the end and getting the associated type. However, I don''t want to reinvent the wheel, but I couldn''t find anything of the kind in MSDN.

Share this post


Link to post
Share on other sites
I suppose it depends on whether you just get the name, or the fully qualified name. Because there''s also an IsArray property of Type, and I can only assume you''d use that instead of checking the name

Upon closer inspection, maybe the static method Type.GetTypeArray is something you''d be interested in.

Share this post


Link to post
Share on other sites
I''m having another problem. Now I just need to know the length of each dimension. However, the Length property returns the number of elements in ALL the dimensions and I can''t find a property/method that does what I want.

Share this post


Link to post
Share on other sites