Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using select case statement with strings

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do I use a select case statement with strings?
 
What I do is that I use a function to convert a string into a mathematical representation, that is an integer. In that convert function each character that I want to influence the mathematical representation, has a certain number that's added to a temporary variable. That is, the function runs through each character in the string passed to it and checks if S='A' then Result:=Result+1, if S='B' then Result:=Result+2, and so on... That way I can use static integers in a case statement, knowing that those integers will represent certain strings. I recommend that you write a simple test program with a converter function of your own, to code it the way you want it to be. You may want to declare it as SIndex(S: string): Integer; and use it this way in its target environment (game engine, script engine, etc):
I:=SIndex(MyCommandString);
//Process engine commands...
case I of
1018: begin {Do something here} end;
98: begin {Do something here} end;
//...and so on...
end;

You'll have to test and see how you want your "string index" converter routine to be - maybe you want it to produce unique integers for unique strings, or maybe you want to apply some other rules. Thing is that if you're aiming for a function that returns a unique integer for case-insensitive (or case-sensitive) strings, you may want to verify that your math in that function does exactly that, so you don't end up making it return the same result for 'AABB' and 'BBAA' and 'ABBA', to take an example.

There are also other ways to produce identifier integers for a case statement to represent strings, wich may be found at other Delphi site forums...

-Steinar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top