Seach for a value in multi-dimensional array in 2005/2008

R

Rick

object[,] siblings = { { "John", 25 }, { "Mike", 30 }, { "Tom", 40 } };

Let say I have a value:
string searchString = "Mike";

How do I search for the [searchString] in the array siblings and get the age
(in case of Mike, it is 30)?
 
J

Jeff Johnson

object[,] siblings = { { "John", 25 }, { "Mike", 30 }, { "Tom", 40 } };

Let say I have a value:
string searchString = "Mike";

How do I search for the [searchString] in the array siblings and get the
age
(in case of Mike, it is 30)?

Is your array always 2-dimensional?
 
P

Peter Duniho

Rick said:
object[,] siblings = { { "John", 25 }, { "Mike", 30 }, { "Tom", 40 } };

Let say I have a value:
string searchString = "Mike";

How do I search for the [searchString] in the array siblings and get the age
(in case of Mike, it is 30)?

It would be much easier if you follow Arne's earlier advice to store the
data in a single structure.

But, if you insist on the 2-dimensional array, it's as simple as
iterating over one dimension, examining the 0th element for each
iteration (which is a string) and comparing it to the string you're
looking for. Once found, the value is in the 1st element for the same
iteration index.

Pete
 
R

Rick

Yes, it is always 2-dimensional.

Jeff Johnson said:
object[,] siblings = { { "John", 25 }, { "Mike", 30 }, { "Tom", 40 } };

Let say I have a value:
string searchString = "Mike";

How do I search for the [searchString] in the array siblings and get the
age
(in case of Mike, it is 30)?

Is your array always 2-dimensional?


.
 
R

Rick

Would you be able to provide me with a sample code? thank you

Peter Duniho said:
Rick said:
object[,] siblings = { { "John", 25 }, { "Mike", 30 }, { "Tom", 40 } };

Let say I have a value:
string searchString = "Mike";

How do I search for the [searchString] in the array siblings and get the age
(in case of Mike, it is 30)?

It would be much easier if you follow Arne's earlier advice to store the
data in a single structure.

But, if you insist on the 2-dimensional array, it's as simple as
iterating over one dimension, examining the 0th element for each
iteration (which is a string) and comparing it to the string you're
looking for. Once found, the value is in the 1st element for the same
iteration index.

Pete
.
 
P

Peter Duniho

Rick said:
Would you be able to provide me with a sample code? thank you

Here is a short program illustrating how to use a multi-dimensional array:


using System;

namespace TestMultiDimensionalArray
{
class Program
{
static void Main(string[] args)
{
object[,] siblings = { { "John", 25 }, { "Mike", 30 }, {
"Tom", 40 } };

Console.WriteLine("There are {0} siblings in the array",
siblings.GetLength(0));
for (int i = 0; i < siblings.GetLength(0); i++)
{
Console.WriteLine("Name: {0}, Age: {1}",
siblings[i, 0], siblings[i, 1]);
}

Console.ReadLine();
}
}
}
 
V

vanderghast

with anonymous type (and LINQ) :



var siblings = new[ ]{
new { name= "Mike", age= 28} ,
new { name= "Mary", age= 25},
new { name= "John", age= 31}
};
int MikeAge = (from s in siblings where s.name == "Mike" select
s.age).Max();


Note that it assumes there is one and only one Mike !



It would be safier to use

var AllMike = from s in siblings where s.name=="Mike"

then to test the count

if( 1 != AllMike.Count( ) ) { }


Or if you already know that the age must be 28:


if( 1== (from s in sibligns
where s.name == "Mike"
&& s.age==28
select s.age ).Count( ) )
{
}




Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top