lamda not finding string

Given that a.Value is a string and dr is a DataRow

Just curious why this works:

 var temp = Formats.Find(a => a.Value == (string)dr[0]);

And this doesn't

var temp = Formats.Find(a => a.Value == dr[0]);

The only difference is that I am not casting it.

If the 2nd one was a casting problem, I would expect an error - but I don't 
get one.  Temp is just = null.

Thanks,

Tom 


0
tshad
1/27/2010 2:00:19 AM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

4 Replies
677 Views

Similar Articles

[PageSpeed] 20

On 1/27/2010 10:00 AM, tshad wrote:
> Given that a.Value is a string and dr is a DataRow
>
> Just curious why this works:
>
>   var temp = Formats.Find(a =>  a.Value == (string)dr[0]);
>
> And this doesn't
>
> var temp = Formats.Find(a =>  a.Value == dr[0]);
>
> The only difference is that I am not casting it.
>
> If the 2nd one was a casting problem, I would expect an error - but I don't
> get one.  Temp is just = null.
>
> Thanks,
>
> Tom
>
>

I haven't look at the documentation, but my guess is
- dr[0] returning an object type, so you need a cast.
- assuming Formats is of type List<T>, then Formats.Find will return a 
default(T) when the search failed. If 'a' is of reference type, will you 
get a null.

Regards.
0
kndg
1/27/2010 2:11:05 AM
On 26-01-2010 21:00, tshad wrote:
> Given that a.Value is a string and dr is a DataRow
>
> Just curious why this works:
>
>   var temp = Formats.Find(a =>  a.Value == (string)dr[0]);
>
> And this doesn't
>
> var temp = Formats.Find(a =>  a.Value == dr[0]);
>
> The only difference is that I am not casting it.

The first does string==string, which checks if the strings
contains the same char sequence.

The second does object==object, which checks if it is the same
object.

Arne


0
ISO
1/27/2010 2:13:38 AM
"kndg" <reply@this.newsgroup> wrote in message 
news:%23Fu9uZvnKHA.4512@TK2MSFTNGP04.phx.gbl...
> On 1/27/2010 10:00 AM, tshad wrote:
>> Given that a.Value is a string and dr is a DataRow
>>
>> Just curious why this works:
>>
>>   var temp = Formats.Find(a =>  a.Value == (string)dr[0]);
>>
>> And this doesn't
>>
>> var temp = Formats.Find(a =>  a.Value == dr[0]);
>>
>> The only difference is that I am not casting it.
>>
>> If the 2nd one was a casting problem, I would expect an error - but I 
>> don't
>> get one.  Temp is just = null.
>>
>> Thanks,
>>
>> Tom
>>
>>
>
> I haven't look at the documentation, but my guess is
> - dr[0] returning an object type, so you need a cast.
> - assuming Formats is of type List<T>, then Formats.Find will return a 
> default(T) when the search failed. If 'a' is of reference type, will you 
> get a null.

I understand the casting issue.

But normally, you get an error if you don't cast it.  In this case, it went 
through normally.

Thanks,

Tom
>
> Regards. 


0
tshad
1/27/2010 5:58:22 PM
tshad wrote:
> [...]
> But normally, you get an error if you don't cast it.  In this case, it went 
> through normally.

You also don't get an error if you do this:

   void MethodA(object obj) { }

   void MethodB()
   {
     string str = "Foo";

     MethodA(str);
   }

Same thing is going on in your code example.  There's no need to cast if 
there's an applicable method for the type(s) given.

More generally, upcasting (to the less-derived types) does not actually 
require an explicit cast.  The implicit cast always exists.  You only 
need an explicit cast when downcasting, to more-derived types.

Pete
0
Peter
1/27/2010 6:21:50 PM
Reply:

Similar Artilces:

Re: FInd the unknow
Could anyone help, any functions (except solver) and formular in excel could find the " a ", " b" & " c " thanks a lot! 1.258333 a + 1.516667 b + 1.688889 c = 21200 1.775a + b + c = 21200 a + 2.55b + c = 21200 a + b + 3.066667c = 21200 ...

Unexpected Error- Running Advance Find Query
I am getting a message that says "Unexpected Error An Error has occurred" when I am running an advanced find query on two custom entities. It seems to be when I have certain custom fields included in my view that the query errors out. Any help on this would be greatly appreciated. Thanks so much. Just wanted to add that I seem to get errors when there is a 0.00 amount in these fields... "TJ" wrote: > I am getting a message that says "Unexpected Error An Error has occurred" > when I am running an advanced find query on two custom entities. It se...

2004 updaters can't find Office
Version: 2004 Operating System: Mac OS X 10.4 (Tiger) Processor: Intel Was there ever any resolution to the earlier thread about this problem? Our employer advises us to update 2004 to 11.5.4, which I've been trying to do on my wife's MacBook. Her Component Plug-in has a version number of 10.1.6. Both 11.3.5 and 11.5.0 are supposed to be combo updaters but running either produces an error message to the effect that the updater can't find a version of Office 2004 to update. On Microsoft's website I haven't been able to find anything earlier than 11.3.5, say 11.3.0 or 10.som...

Problem using Find and Replace ,B with ,C within an Excel Formula
Hi, I am trying to replace ,B with ,C in the following formula and get an incorrect result. =SUMPRODUCT(BB$2:BB$377,BL$2:BL$377)/(SUMPRODUCT(BB$2:BB$377,BO$2:BO$377)+SUMPRODUCT(BB$2:BB$377,BG$2:BG$377)+SUMPRODUCT(BB$2:BB$377,BH$2:BH$377)+SUMPRODUCT(BB$2:BB$377,BI$2:BI$377)+SUMPRODUCT(BB$2:BB$377,BJ$2:BJ$377)+SUMPRODUCT(BB$2:BB$377,BK$2:BK$377)+SUMPRODUCT(BB$2:BB$377,BL$2:BL$377)+SUMPRODUCT(BB$2:BB$377,BM$2:BM$377)+SUMPRODUCT(BB$2:BB$377,BN$2:BN$377)) When I do a find and replace of ,B with ,C Excel replaces the :B with :C instead of replacing ,B with ,C . See the result below after the fin...

Remember advanced find settings
Is there a way I can get Outlook to remember my advanced find settings? I use the facility a lot to interrogate my emails. Every time I select advanced find I'd like to remember certain fields and window position. Thanks. ...

Problems with Find in Archive folders...?
Howdy, I am in OL2000... I just ran the Find function on my Sent Items folder, did not find the message I needed, and realized that it probably had been archived. I then ran the Find on the Archived Sent Items folder. In a moment, it flashed "Done" but came up empty. I then scrolled down and quickly found the message that I knew to be there. Does the Find function not work (properly) on archived folders? If not, how does one find stuff? Sincere thanks, -- Kenneth If you email... Please remove the "SPAMLESS." ...

macro to Find Replace in Excel
Hi, I need to find double quotes in an excel file (could be a selectoin of rows) and replace them with nothing ( yeah just want to delete them) The built in Excel function to Find and Replace all gives me error "formula too long" Can someone please write me a small macro code thanks Hi I just tried with Find and Replace Replace All " with nothing entered removes all " from sheet/range Replace All "" with nothing entered removes all "" from sheet/range, but entries like " or " " remain unchanged. Arvi Laanemets "Nurddin&quo...

Find a file
How do I search for a file in Word 2007? You search for it in Windows. Click Windows key + F to display Search. -- Stefan Blom Microsoft Word MVP "malkovic" <malkovic@discussions.microsoft.com> wrote in message news:6AB4A204-1A41-48AB-A9E8-919D78A9C0C2@microsoft.com... > How do I search for a file in Word 2007? ...

Finding proper range to join rows.
XL2000, Windows NT 4 I have two instances in a worksheet where I need a macro to join several rows in column A. In the first instance, the first row I want to select generally starts with or contains the string "E--" (without quotemarks) in the first row. The selection of rows should end before a row that contains several spaces and the string "YTD" (without quotemarks). I have a macro to join the selected rows but I'm not sure how to make the proper selection. In the second instance, the next section of rows I want joined generally starts with or contains the strin...

Mid string null value
I'm using the code below to display everthing after the pound sign (#) and it works for the most part, but displays the whole field for fields that don't have a pound sign. My goal is to display everything after the # if there is a #, otherwise leave the field blank. Mid([Description],InStr([Description],"#")+1) Thanks, Jim Jim wrote: > I'm using the code below to display everthing after the pound sign > (#) and it works for the most part, but displays the whole field for > fields that don't have a pound sign. > > My goal is to di...

Another string manipulation question
I have this code: If Me.Text2 < Me.EndDate Or Len(Nz(Me!Text2, "")) = 0 Then msgbox "testing" endif What i'm trying to do is to check the string text2 for two variables. 1 is it less than the EndDate 2 is it empty or does it have something in it. I keep getting the error invalid use of null. I'm not sure the code is correct. I thank you for checking this and reading this. -- Message posted via http://www.accessmonster.com Lots of ways you could do it. Error is on the < compare when one side of the equation is a null How about ...

how to convert CString and std::string to number?
How to determine if the CString and std::string is a number and how to convert it to number? int i = atoi(std::string::c_str()); > How to determine if the CString and std::string is a number and how to > convert it to number? -- new A tricky way to do this for CString would be to use: { CString cs = _T("124.3"); if(bIsNumber(cs)) { float d = _tstof(cs); // Do other stuff } } BOOL bIsNumber(const char *csStr) { const char *myNumber = _T("0123456789. "); CString cs = csStr; return(cs.Trim(myNumbers).IsEmpty()); // All n...

Add/Remove Programs not finding all programs
I am trying to remove Blackberry Messenger and Add/Remove programs doesn't find it. It is installed in the Program Files folder. I've looked around to see if there was a product unique file removal program but saw none I recognized. Anyone have any thoughts? "Berg" <Berg@discussions.microsoft.com> wrote in message news:E5F99188-0851-4FC4-877E-346C499A5A0B@microsoft.com... > I am trying to remove Blackberry Messenger and Add/Remove programs doesn't > find it. It is installed in the Program Files folder. I've looked around to > see if ...

HOw to find who connected to database at what time
I am still on SQL Server 2000. I know Oracle pretty well but learning SQL Server. In Oracle there is a listere log which shows who made databas econnection at what time. Is there something similar in SQL Server. See if this gives you what you need: EXEC sp_who2 -- HTH Kalen ---------------------------------------- Kalen Delaney SQL Server MVP www.SQLServerInternals.com "Prem Mehrotra" <PremMehrotra@discussions.microsoft.com> wrote in message news:D5911D7A-4D65-4398-A228-7ADD37CE02E7@microsoft.com... > I am still on SQL Server 2000. I know Oracle pr...

Find out if maximized
How do I find out if aView has been maximized ? I tried pView->IsZoomed() but that always returned 0 Hi, Use GetWindowPlacement and test the showCmd member of the WINDOWPLACEMENT structure. HTH -- ============ Frank Hickman NobleSoft, Inc. ============ Thanks for the advice,but it doesn't work in my app. I've got an MDI-app. CMainFrame has been maximized but CChildFrame hasn't. Now I open a View. This appears as a 'normal' window.Now I maximize my view and see it really maximizes and covers the whole clientarea of the CMainFrame. Now I right click on its surfa...

Finding my extended download license information...
Version: 2008 Operating System: Mac OS X 10.4 (Tiger) Processor: Intel I just installed my 2008 version of Mac. I also purchased the $12.99 extended download service. I will likely get a new Mac in 6 months and want to be able to download this version of Microsoft word on my mac. How do I install this version on my new mac. What link can I go to and install the download? My order number is 5520482116. I did not receive any information about the &quot;Extended download service&quot; or an email to that effect as to how I use the service. ...

Find Button 10-12-07
Hi, I have a form (call this Aform) which lists results from a query; but I also want to add a find option to the form so that the user can just enter in the unique ID ( which is display on the result form). When the user clicks find, it searches 6 other different types of forms (more detail information on these forms). If it finds the ID, it opens the new form (Bform) and displays that record on Bform. I tried DoCmd.OpenForm but I couldn't get it to work. Thanks ...

Excel truncates text strings of spaces?
Hi, I have a large table in an Access project (.adp), one field of which is a 20-character alpha-numeric code, where each character signifies something. The code may contain internal spaces i.e ABCDEFGHIJ 12345678 where char's 11 and 12 are spaces. However, when I copy the data into Excel some (not all?) of the internal spaces are truncated, leaving me with 18 and 19 character codes? Does anyone know why Excel does this and how I can prevent it. I'm investigating ways to possibly prevent it on the MS Access side but would welcome any Excel-specific advice. Many thanks. Inse...

Need formula to locate and validate specific text from a string
Hi, I have the following sample data in column A. germany (testing) Germany (cold) Germany Austria (Testing) Austria (cold) Austria (working) Austria (other) China China (alternate) In column B, I want it to show that if the cell to the left has any of the words 'testing' or 'cold' or 'working' it should show "yes" in Column B else show "no". Is this possible? Regards, Vinstream Hello, Try; =IF(OR(ISNUMBER(SEARCH("testing",A1)),ISNUMBER(SEARCH("cold",A1)),ISNUMBER(SEARCH("working&qu...

Find and delete text in a table
Hi all, I have a table say 10 columns long by 5 columns high. There may be text repeated sporadically throughout the table. As an example, if I decide that row 1 is "negative" then I want each text field (10 columns) in row one and all the text fields like them thoughout the table to be deleted. But, alas, I can't figure out how to do it. Any ideas? Thanks, Dave On Nov 18, 10:29=A0am, Dave <Dave.Bea...@lhsc.on.ca> wrote: > Hi all, > > I have a table say 10 columns long by 5 columns high. There may be > text repeated sporadically throughout the table. As an ...

Instr parsing with a comma delimited string
Hi all - I'm using the InStr function to parse a comma delimited text field for use in a parameter query. It works like a charm, except that the data in the field is very small and it's splitting by character. For example, here's the function I'm using in the query SQL (I snipped the rest of the SQL for space): InStr(1,[Forms]![frmAppliesTo].[txtSelections] & ",",[BOOK_ID]) Works great if what's in the text box is: 1 3 1,3 If, however, the text box is "13", it returns records for 1, 3, and 13. Any hints on how I could fix ...

Finding Adjacent empty cell
lets say you have 1 column (A) of data and it goes to row 5, You inser a new Column A and shift everything to the right. So now you have blank Column A and a Column B that has 5 rows of data. How do I find the cell in Column A that would match the last row o data in Column B which would be 5, So that i can fill Column A wit data I was thinking i can find the last cell of data in Column B. could than move the cell selector over to the left by 1 and fill up? And i so how do i do that? Thanks in advance. I hope it is clear Oh i'm using Excel Xp Thanks Keit -- s1i -------------------...

Find Second Blank Row
Hi, help would be appreciated on this please: I currently have a macro that will copy the contents of a couple of rows on another worksheet when a button is pressed. However, the macro copies the data on the same row as which it was originally recorded. What I want it to do is to work down from the top and copy the required rows once it finds 2 blank rows (in a row so to speak). So that in effect there is a blank row between each group of rows with contents in them. I presume I need to add a line if code to tell it to do that. I hope that makes sense. Thanks for advance. Nejl, Gener...

Find and replace
Hi, I'm having a problem with "find and replace" With Italics. If my cell is a mixture of italics and regular font text and I do a find and replace of a single word, it automatically changes the formatting to all italics. Does anyone have a solution for these problems? I have searched HELP and I have searched the Microsoft Excel and Microsoft Office web sites. NOTHING is discussed on this formatting problem. HELPPPP!!!! Thanks. Save your file and then try this find and replace macro. The macro checks the first character of the word/phrase to be changed. If this i...

Crystal Reports 9.2.2? Where to find
I have Visual Studio .NET installed, which apparently has 9.2.0 of Crystal Reports with it. When I try to install the Crystal Reports Enhancements with CRM, it is of course looking for 9.2.2. I cannot find an upgrade path anywhere.... Anyone have advise on where to look? Thanks in advance, Jim Try contacting Business Objects www.businessobjects.com . 9.2.2 is the latest and last version of Crystal v9. I dfounf their N Sydney Office very helpful. Hope that this helps Gill >-----Original Message----- >I have Visual Studio .NET installed, which apparently has >9.2.0 of...