temporalcohesion.co.uk

from the mind of…

Archive for July, 2007

Limitations in Delphi

without comments

One of the things in Delphi that frustrates me is the inability to match on strings in case statements. For those people who haven’t done delphi before, case statements are very similar to c++ switch statements, and only opererate on ordinal types.. Now I’ve not done any Java, but as I understand it you can’t switch on strings in that language either. You can in c# though.

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#.

Anyway, that’s enough rambling. On to the solution.

What you need to do in order to get around this limitation is cheat. Well, it’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.

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

See! That’s how simple it is.

Maybe in a future version of delphi borland/codegear will introduce native string support for case statements. I won’t hold my breather though.

Written by stuart

July 23rd, 2007 at 10:13 pm

Posted in delphi

Hello world!

with one comment

Welcome to my new website, and my new blog. I’d kind of outgrown the old site, which I’ve now turned into a kind of “here’s my CV” sort of site. Although I am happy in my job.

I’m going to attempt to write more often on here, about a lot of things. I’m still getting things just the way I like them - I’ve still got a few plugins for wordpress to download and install yet. If I get time I might even write my own theme, but considering there are so many already brilliant themes available I might just save myself the time and effort.

Hell, that’s why I’m using Wordpress and not writing my own blogging system.

As an old friend of mine used to say: Onwards and Upwards!

Written by stuart

July 20th, 2007 at 1:09 am

Posted in News