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

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)?
0
Utf
5/17/2010 3:13:01 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

6 Replies
516 Views

Similar Articles

[PageSpeed] 48

"Rick" <Rick@discussions.microsoft.com> wrote in message 
news:E4E7F5C2-88A4-4D32-9F19-4C1DCA61B898@microsoft.com...

> 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? 


0
Jeff
5/17/2010 3:27:23 PM
Rick wrote:
> 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
0
Peter
5/17/2010 4:10:38 PM
Yes, it is always 2-dimensional.

"Jeff Johnson" wrote:

> "Rick" <Rick@discussions.microsoft.com> wrote in message 
> news:E4E7F5C2-88A4-4D32-9F19-4C1DCA61B898@microsoft.com...
> 
> > 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? 
> 
> 
> .
> 
0
Utf
5/18/2010 4:30:01 PM
Would you be able to provide me with a sample code?  thank you

"Peter Duniho" wrote:

> Rick wrote:
> > 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
> .
> 
0
Utf
5/18/2010 4:31:01 PM
Rick wrote:
> 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();
         }
     }
}
0
Peter
5/19/2010 6:57:53 AM
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


"Rick" <Rick@discussions.microsoft.com> wrote in message 
news:E4E7F5C2-88A4-4D32-9F19-4C1DCA61B898@microsoft.com...
> 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)? 

0
vanderghast
5/20/2010 3:29:24 PM
Reply:

Similar Artilces:

Server 2008 - VPN - DNS
When a VPN connection occurs domain server gets an extra IP and local PCs resolve server-dc as VPN IP then problems appear. server-dc has 3 IPs now: VPN host: 192.168.35.90 LAN: 192.168.35.210 WAN: 88.255.136.46 We have configured many programs to use server-dc as remote end, ie. to make sql connection. SQL server listens only LAN IP. How can I configure DNS to responds server-dc as LAN IP always. I removed VPN IP but it will be created again later... I removed WAN & VPN IPs and disabled Dynamic Updates for server-dc.local Is it OK? Negative, these IPs...

Hiding the "#N/A" value
How does one hide this value so that it isn't visible in a cell. Also; it appears that when I try a simple calculation of cells; and one of those cells has this value listed in it; my calculation does not complete. How can I correct this? 1. =IF(ISNA(myformula),"",myformula) 2. Usually, it means one is formatted as text or has spaces in it or something. ******************* ~Anne Troy www.OfficeArticles.com www.MyExpertsOnline.com "theslaz" <slaz@home.com> wrote in message news:NZ2ue.73345$on1.9727@clgrps13... > How does one hide this value so that it is...

Seach for a value in multi-dimensional array in 2005/2008
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)? "Rick" <Rick@discussions.microsoft.com> wrote in message news:E4E7F5C2-88A4-4D32-9F19-4C1DCA61B898@microsoft.com... > object[,] siblings = { { "John", 25 }, { "Mike", 30 }, { "Tom", 40 } }; > > Let say I have a value: > string searchString = "Mik...

DPM 2007 does not see SQL 2005 Analysis Services Database
Hi ! We have DPM 2007 latest hotfix running and protecting a server with SQL 2005 and SQL 2000 installed. Also Analysis Services for SQL 2005 installed on that server which has a database mounted. When modifying the protection group I see the SQL 2000 instance and the SQL 2005 instance but I do not see the Analysis Services Database or instance in DPM. How can I backup that database with DPM ? Thanks for your help, Alex ...

Money 2005
Hello! Please apologize my poor English! But there is no German Group for this product. Yesterday I got a new Money 2005 bought by Amazon. After installing and while I was preparing a new bank account I got the offer to update. I canceled this update and worked, imported datas from my bank and so on. All worked correct! Maybe an hour or so later I got again the offer to update and I accepted. After updating, closing und performing a new start of Money I always get the error message - "Runtime Error! Program: C:\Programme\Microsoft Money 2005\MNYCoreFiles\msmoney.exe R6025 - pure vi...

Array of structures
Hi How to implement an array of structures? Can I use CArray or CList? Thanx. hi Depends on what kind of functionality you want. If you know the number of elements you want to store then you can use CArray, else use the CList. Using a CList typedef CList<mystruct*, mystruct*> mystructList; mystructList myList; myList.AddTail(&mystructelement); hth gangadhar Tina wrote: > Hi > > How to implement an array of structures? Can I use CArray > or CList? > > > Thanx. ...

Date Values
I would like to be able to enter a date as 25.12.2003 and in the next cell over have it formated as December 25, 2003 If there a simple way to do this? Thanks for any help. Sharlene England If your regional settings have a short date style of 25/12/2003, you could enter this formula in the adjacent cell: =SUBSTITUTE(A2,".","/")+0 Select the cell with the formula Choose Format>Cells On the Number tab, select the Custom category Clear the text box, and type: mmmm d, yyyy Click OK Sharlene England wrote: > I would like to be able to enter a date as 25.12.2...

Changing combo to list box for multi select values
Hi all, can anyone help I have a variable listbox, which when i click another list box it fills the first listbox with value relating to my other list box eg when i click customer in one list box it fills the another with all customer name, when i click sales person it fills it with sales people names now what i need it to be able to select multi values in this listbox, it is not bound to any columns as it a variable, how do i get it to select multi value and where would i place this in my code for example would it be after update to that list box Thanks in advance ...

I need to hide points on a line chart if the value is ZERO
I'm using a line chart to display my info. I only have to show the points on the graph. I found where to hide the line so only the points show. The only problem is if I have a value of zero it's displaying a point at ZERO on the axis, this is not what I want. I can't find where / how to (remove or hide) point if a value of ZERO. Is this possible? Please advise Hi, If you do not want the zero to show you can do one of the following - delete it and have empty cell instead - type #N/A - use formula =NA() Select chart and use Tools > Options > Chart and make sure the Pl...

Birthdays appearing as Multi-day event
I've got a problem with Birthdays in Outlook 2003. They used to appear as an all day event with the age at that birthday in brackets next to the name. Now it appears as a multi-day event. Why has this happened and how can I solve the problem? Thanks in advance. Will Will <wgodwin@gmail.com> wrote: > I've got a problem with Birthdays in Outlook 2003. They used to appear > as an all day event with the age at that birthday in brackets next to > the name. Outlook can't do that. Pocket Outlook on a Windows Mobile PDA can, however. > Now it appears as a multi-d...

Adding value to combo box..
I have a combo box thats rowsource is a table. How can I be able to add new entries into the combo box without going back into the table itself? Stephen -- Message posted via http://www.accessmonster.com There is an event on the combobox called "On Not in List" that fires when new data is entered that is not in the datasource. I suggest using this event to give the user some options about the data, making sure the user really wants to add that data to the table and it's not just a typing error. You can also put some validation in to make sure the data looks like you ...

Making field not required based on other field value
Hi all, Field "Street 1" on the Account form is defined as Business Required. I'd like to keep that way some of the time but change it based on the type of the account. This thread explains something similar but I could not get it to work for me. http://groups.google.com/group/microsoft.public.crm/browse_thread/thread/479ac315f13fa2f9/e54b1fb36bff8844?lnk=gst&q=required+business+field&rnum=2&hl=en#e54b1fb36bff8844 I'd like to keep the attribute definition as Business Required. When an account is opened to be viewed, if a certain picklist is populated, I want to...

How can i copy a value in a cell from a sheet and paste in anothe.
Hello, I ask my question with an exemple : I want to copy a value in C2 from sheet1 to D5 on sheet2. But , if i modify C2 on sheet1, D5 sheet2 has to be modified too. Do you know the function to do that? Thanks :) ( Sorry for my bad english, i speak french usually ) Try putting this in D5 on sheet2: =Sheet2!C2 Maybe this is what you want. Art Hi use the formula ='sheet1'!C2 -- Regards Frank Kabel Frankfurt, Germany "clapton" <clapton@discussions.microsoft.com> schrieb im Newsbeitrag news:F401E623-B2F0-463C-8E0E-04874B24F195@microsoft.com... &...

#Value!
Hi In cell A1 I have a vlookup formula and =if(sheet1A1="","",sheet1a1) which returns the correct result when blank. In M1, I have the formula =if(a1="","",networkdays(k1,k3))-1 This also returns the required info when sheet1A1 has something in the cell, but when sheet1A1 is empty, M1 returns #VALUE! I have tried in M1 =if(iserror(networkdays(k1,k3))-1 etc, but get the same result (#VALUE!) any help appriciated to tidy up the spreadsheet by removing #VALUE! regards Brian -- Message posted via OfficeKB.com http://www.officekb.com/Uwe/Forums.aspx/...

find highest values
hi, i have 2 sets of data on a spreadsheet both contain data about the number of occurrence a machine went into fault ie. grid 1 OP Occurr op 10 7 Op 30 9 Op 40 3 grid 2 OP Occurr op 10 9 Op 30 4 Op 40 14 what i want to do is compare the two sets of data and which ever is the greater place in another grid Result OP Occurr op 10 9 Op 30 9 Op 40 14 what the best way of doing this ? thanks kevin =MAX(VLOOKUP(J2,grid1,2,FALSE),VLOOKUP(J2,grid2,2,FALSE)) -- HTH Bob (there's no email, no snail mail, ...

Nothing about Dynamics GP 10.0 Upgrade SQL Server 2005 to 2008?
Good evening. We previously received a Microsoft SQL Server 2008 standard installation disk from Microsoft and that is it. I can not find any clear instructions on how to take upgrade a GP 10.0 running on Win Svr 2003 with SQL Server 2005 to SQL 2008. Any thoughts? Is it just back up your data, stick the disk in and follow the prompts? It can not be that easy. :-) -- Thanks, KevB KevB, My 2 cents would be to not upgrade an existing SQL Server instance. Instead I would install a new instance of SQL Server 2008 and follow the instructions in this KB article: https://mbs...

array formula to provide all values associated with a given value
From an Access Query. I've got a list with the unique ID's for reports, and the next field indicates what Language they were written in. This was from a Many - Many relationship in Excel, and many of the reports were written in many different languages. ReportID CountryID 1000020 Td 1000020 Tz 1000020 Ug 1000020 Zm 1000020 Zw 1000035 Ao 1000035 Mz I've got over 10,000 records like this and would like an array formula that for each Report ID (and i'...

Open - Tools
Hi all When you use the open function, then you have a dropdown called Tools, and here you can pic SEACH. I have upgraded from Excel 2002(XP) to Excel 2003 - maybe it is me, but what worked fine in excel 2002 seams not to be working in excel 2003. What could I be doing wrong or does the function not work ? Thanks, Flemming Works for me okay. Not that I use this much. Specific steps to recreate the problem? -- Jim Rech Excel MVP "Flemming Dahl" <svineheld@sport.dk> wrote in message news:88d0466f.0411100113.7b61459e@posting.google.com... | Hi all | | When you use the o...

#VALUE error-- some cells?
I am doing a VLOOKUP moving out comments from a Pivot Table to separate column. I have 115 rows, 100 of which work perfectly... but 15 of them give me the #VALUE error stating "A value used in the formula is of the wrong data type." The formula I am using is: =IF(VLOOKUP(LEFT(C19,2500),$C$5:$J$64,1,FALSE)=0,"",(VLOOKUP(LEFT(C19,2500),$C$5:$J$64,1,FALSE))) Why does it work for some but not others? Some of the ones not working have acronyms, HR, CC'd, etc... could that be the problem-- and if so, how can I get it to ignore them? Thanks! Did you reall...

Modify code from paste values to change font color / multiple rang
Hi, I was wondering if you could help me out modify a code that currently paste values to change the font color to automatic or black and to add multiple ranges (new ranges are E8:E17, E24:E33,E40:E49 and E56:E81) The current code is below and works Great! Thanks to everyone that helped/will help me out!! Sub PVPrImpct() With Range("b48:b74").Offset(, Worksheets( _ "Summary by month MTD").Range("A4") - 1) .Value = .Value End With End Sub Thanks again! I'm not sure if those ranges are before the offset or after... ...

Countif if the value falls between 2 other values
Need to count values that fall between two limits: for example 462 997 569 122 Need to count values between 300 and 600. I can do if my desired value is over or under a target, but not within a range. Any help would be greatly appreciated. Thanks. =COUNTIF(A:A,">=300")-COUNTIF(A:A,">600") -- Regards, Peo Sjoblom "les8" <les8@discussions.microsoft.com> wrote in message news:6E83BE3D-E5A5-4114-AFDB-CC685F1DB5EF@microsoft.com... > Need to count values that fall between two limits: for example > 462 > 997 > 569 > 122 > Need to co...

Lookup cell above next value >0
I have a table with two rows, 33 columns long. Top row has Years (2004 to 2036) in it and the row below is intended for the inputing of values in percentage form. Some of the cells in bottom row may be left blank or with 0 value. Elsewhere on my spreadsheet I want to create formulas that will return the top row value (year) for each year where the value in bottom row is greater than 0. For example, if A1 = 2004 A2 = 2005 A3 = 2006 A4 = 2007 B1 = 0 % B2 = 5 % B3 = blank B4 = 20% What formula in cell C1 will return value "2005" and in cell C2 the next year ...

multi criteria code
hello every one I have this code from Northwind sample. Private Sub ReviewProducts_Click() ' This code created in part by Command Button Wizard. On Error GoTo Err_ReviewProducts_Click Dim strMsg As String, strTitle As String Dim intStyle As Integer Dim strDocName As String, strLinkCriteria As String ' If CompanyName control is blank, display a message. If IsNull(Me![CompanyName]) Then strMsg = "Move to the supplier record whose products you want to see, then press the Review Products button again." intStyle = vbOKOnly strTitle ...

Vlookup not working where lookup value contains an apostrophe
Column A contains a list of names including O'Brien. Column B contains the result of a vlookup which uses the names in column A as the look up value. ie Column B2 contains the formula =vlookup(A2 ... The Array table is on a separate sheet within the workbook. All look up values except for O'Brien generate the desired result. O'Brien generates #N/A. Any suggestions as to what is going wrong? It should work fine. Maybe there's a white space somewhere throwing apparent good matches off. Try using TRIM: =VLOOKUP(TRIM(A2),.. Any good? hit the YES -- Max Singapore http://...

Inserting two cell values into a new cell + text
I am likely making this much more difficult than it needs to be... I need to take the numeric values from two cells and insert them into a new cell in this format: "898 - 1398". Where 898 and 1398 are numeric values generated by a formula and are subject to change. What I have is: =VALUE(A1)&" - "&VALUE(A2) but the result gives me one or more decimal places in each number result which I know is a result of the string concatination process. My question is: How can I rewrite this cell's formula to zero out the decimal entries form the source cells which is h...