<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>temporalcohesion.co.uk &#187; delphi</title>
	<atom:link href="http://temporalcohesion.co.uk/tags/the-programmer-in-me/the-bastard-child-of-borland/feed/" rel="self" type="application/rss+xml" />
	<link>http://temporalcohesion.co.uk</link>
	<description>...trying not to write legacy code, man.</description>
	<lastBuildDate>Mon, 12 Dec 2011 08:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Limitations in Delphi</title>
		<link>http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=limitations-in-delphi</link>
		<comments>http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/#comments</comments>
		<pubDate>Mon, 23 Jul 2007 22:13:54 +0000</pubDate>
		<dc:creator>stuart</dc:creator>
				<category><![CDATA[delphi]]></category>

		<guid isPermaLink="false">http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/</guid>
		<description><![CDATA[<a href="http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/" title="Limitations in Delphi"></a>One of the things in Delphi that frustrates me is the inability to match on strings in case statements. For those people who haven&#8217;t done delphi before, case statements are very similar to c++ switch statements, and only opererate on &#8230;<p class="read-more"><a href="http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/" title="Limitations in Delphi"></a><p>One of the things in Delphi that frustrates me is the inability to match on strings in case statements. For those people who haven&#8217;t done delphi before, case statements are very similar to c++ switch statements, and only opererate on ordinal types.. Now I&#8217;ve not done any Java, but as I understand it you can&#8217;t switch on strings in that language either. You can in c# though.</p>
<p>I think C# is generally better, but that said I have it on good authority that templates are still much better in C++ than generics are in C#.</p>
<p>Anyway, that&#8217;s enough rambling. On to the solution.</p>
<p>What you need to do in order to get around this limitation is cheat. Well, it&#8217;s not really cheating. The tricks is to use a look up function that accepts a string, one that you are expecting, which you have in a string array. Then all you need do is return which element of the array has been matched, and use that in the case statement.</p>
<pre lang="delphi">
function TClassName.lookupFunction(lookup: string): integer;
const
  ARRAY = ['one', 'two', 'three', 'four', 'five'];
var
  i: integer;
begin
  for i := 0 to Length(ARRAY) do
    if lookup = ARRAY[i] then
    begin
      Result := i;
      break;
    end
end

procedure TClassName.someProc(somestring: string);
begin
  case LookupFunction(somestring)
  1 : //code that does stuff for 'one'
  2 : //code that does stuff for 'two'
  3 : //code that does stuff for 'three'
  4 : //code that does stuff for 'four'
  5 : //code that does stuff for 'five'
end</pre>
<p>See! That&#8217;s how simple it is.</p>
<p>Maybe in a future version of delphi borland/codegear will introduce native string support for case statements. I won&#8217;t hold my breather though.</p>
]]></content:encoded>
			<wfw:commentRss>http://temporalcohesion.co.uk/2007/07/23/limitations-in-delphi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

