Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Rectangular Array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Trecius - 28 Aug 2007 17:16 GMT
Hello, Newsgroupians:

I have a question regarding rectangular arrays.  Suppose I am trying to
create a rectangular array of type System.Object.  Each item in the
rectangular array may be an integer, a double, or a string.  However, until
runtime, the array items are not known, so it will be a form of late binding.
What's the best way to create such a rectangular array?  The way I can
picture it is as follows...

// For simplicity, assume the array to be 3x3.
object[,] arr = new object[3,3];
arr[0, 0] = 6;
arr[0, 1] = 2.5
arr[0, 2] = "Hello, World";
arr[1, 0] = "Jones";
arr[1, 1] = 36.3";
arr[1, 2] = "Hello, again";
...

Is this the proper way, or should I use an ArrayList that contains an
multitude of ArrayLists?  Or is there a better approach I should be taking?  
Thank you.

Trecius
Nicholas Paldino [.NET/C# MVP] - 28 Aug 2007 17:54 GMT
Trecius,

   Well, if there is any way that you can determine the format beforehand,
perhaps by using a structure, that would be the best option.  If you truly
want to use a rectangular array, then what you have is best.  If you have an
ArrayList within an ArrayList, then that will be a jagged array, and you
could have different dimensions for each row (or column, depending on how
you are looking at it).  You would also access it like so:

// Assuming arr is an array list of array lists.
ArrayList arr2 = arr[0];
object val = arr2[0];

   It would just be a pain, as you would have to pull out the array list
every time.  You might be better off with a List<List<object>>, which would
allow you to access it directly like so:

// Assuming arr is List<List<object>>
object val = arr[0][0];

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello, Newsgroupians:
>
[quoted text clipped - 23 lines]
>
> Trecius
Trecius - 28 Aug 2007 18:10 GMT
Thank you, Mr. Paldino.

> Trecius,
>
[quoted text clipped - 43 lines]
> >
> > Trecius
Mythran - 28 Aug 2007 18:21 GMT
> Hello, Newsgroupians:
>
[quoted text clipped - 23 lines]
>
> Trecius

Just a FYI:

You can declare the object array and assign the elements to the array at the
same time....IE:

object[,] arr = new object[,] {
   { 6, 2.5, "Hello, World" },
   { "Jones", 36.3, "Hello, again" }
};

HTH :)

Mythran

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.