Jump to content
  • Advertisement

Archived

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

yaroslavd

Traversing an array of unknown dimensions

This topic is 5164 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
Advertisement
Add an extra one to the end and put 0s in it to signify its the last one. The same way null terminated strings work.

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
Aren''t you the lucky one Turns out everything is also derived from Object, which implements a GetType method. It will return a Type instance you can mess around with!

Share this post


Link to post
Share on other sites
Right, but it will return the array Type. For example, (new Weapon[]).GetType() will return Weapon[]. I think

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!