Why not start with a "How Stupid..."?

I have been working in both VFP and .NET lately, and recently had a "blonde" moment due to the fact that in VFP, everything is 1-based, whereas in .NET, everything is 0-based.

In VFP, if you want to search for a string within a string, VFP has a function called AT(), which returns the starting point of the string within the string. If the string cannot be found, AT() returns 0.

I wanted to create the .NET equivalent of the following VFP code:

   lcEmailMessageBody = "<html> ...."

   IF AT("<html",lcEmailMessageBody)>0 THEN
      SendHTMLMail(lcEmailMessageBody)
   ELSE
      SendTextMail(lcEmailMessageBody)
   ENDIF


So I create the following .NET equivalent:

   lcEmailMessageBody = "<html>....";

   if(lcEmailMessageBody.IndexOf("<html")>0)
      SendHTMLMail(lcEmailMessageBody);
   else
      SendTextMail(lcEmailMessageBody);


Pretty straightforward, n'est-ce pas?

AAANNNN - wrong! When I ran this code, it only sent text mail.

***HEADSLAP***

Since strings in .NET are 0-based, a return value of 0 from IndexOf() means that the string was found in the first character of the string (character position 0 in the string). A return value of-1 means that the string is not found. so the proper if-statement is:

   if(lcEmailMessageBody.IndexOf("<html")!=-1)

The funny part is that before I got slapped, I created another project to test it there, andthen asked a colleague if he got the same result on his computer. And as the words came out of my mouth, I realized the mistake, and braced myself for the slap.

Comments

Rick Hodder said…
Thanks for the kind words requiscat! Your no slouch yourself - your blog shows that you're well read, and your tastes in music are very eclectic.

Popular posts from this blog

Using Linq To XML to Parse XML with Multiple Namespaces

String Interpolation in Powershell Strings