On-Line Библиотека www.XServer.ru - учебники, книги, статьи, документация, нормативная литература.
       Главная         В избранное         Контакты        Карта сайта   
    Навигация XServer.ru








 

Один из путей использования оператора Case со строками

Здесь описан нестандартный путь использования оператора Сase со строками.

Declare a function StrCase with an open string array. This function checks a selector string against the open string array and gives back the position of the selector in the array (0 to count-1). In case the selector isn't found the result is -1.

function StrCase(Selector: string; StrList: array of string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to High(StrList) do begin
if Selector = StrList[I] then begin
Result := I;
Break;
end;
end;
end;
Now you can use the function StrCase instead of the case selector and you will get a very simple construct.
procedure TestString(StringToTest: string);
begin
case StrCase(StringToTest, ['First', 'Second', 'Third']) of
0: ShowMessage('1: ' + s);
1: ShowMessage('2: ' + s);
2: ShowMessage('3: ' + s);
else
ShowMessage('else: ' + s);
end;
end;
That's all. The advantage is the use of one function for all string cases and the absent af any additional typespecs etc.
Have fun!



Языки программирования: разное