Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieve array from rectangular array

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
GB
Hi,

string[,] a = new string[3,2];
string[] b = a[0]; // error, wrong number of indices (expects 2)

I don't want to retrieve the separate values.

I want to retrieve the arrays. How do you retrieve the first array (b) from the rectangular array (a)?

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
something like
Code:
var x = new string[2,3];
var y = new string[3];

y[0] = x[0,0];
y[1] = x[0,1];
y[2] = x[0,2];
x is a matrix, not a key value pair, which is what it sounds like you want. for that you would use a dictionary
Code:
var Dictionary<int, string[]> x= new Dictionary<object, string[]>
{
   {1, new string[3]},
   {2, new string[3]},
   {3, new string[3]},
}
var y = x[1];

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Well, I want to retrieve a row/array from a matrix :)

I went with jagged array [j] to get the task done but would have felt better to use the rectangular [i,j] notation.

The example you gave would retrieve cells/values rather than rows.

Thanks,

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
I'm curious, why create such low level structures to begin with? Why not create an object which explicitly reflects it' purpose? For example:
Code:
var people = new object[2,3];
people[0, 0] = "Jason";
people[0, 1] = 30;
people[0, 2] = Sex.Male;
people[1, 0] = "Lindsey";
people[1, 1] = 29;
people[1, 2] = Sex.Female;
is very difficult to understand. on the other hand
Code:
class Person
{
   public string Name {get;set;}
   public int Age {get;set;}
   public Sex Gender {get;set;}
}
var people = new [] {
        new Person {
              Name = "Jason",
              Age = 30,
              Gender = Sex.Male,
           },
        new Person {
              Name = "Lindsey",
              Age = 29,
              Gender = Sex.Female,
           },
      }
expresses the intent in explicit terms. i can easily see what is happening.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top