Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Общие вопросы > Case...of


Автор: Spawn 4.7.2002, 19:52
В операторе case можно выбирать из значентий типа Integer:
case chislo of
1:
2:
3:
end;
Но если использоать так значения типа String, то возникает еррор:
case slovo of
'la':
'la-la':
'tra-la-la':
end;
Как можно сделать выбор из значений типа String?

Автор: Vit 4.7.2002, 19:53
Только вложенными if  :(  :(  :(  :(  :angry:  :angry:

Автор: podval 5.7.2002, 12:59
не помню, где нашел:

Код

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!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)