Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.08.19;
Скачать: CL | DM;

Вниз

Изящно, как case of, но со string... как?   Найти похожие ветки 

 
Vlad Oshin ©   (2007-07-20 11:27) [0]

case a of
case1:
case2:

а со строками?

не писать же
b:=a;
if a = "case1" then
if a = "case2" then
...
if a = "case28" then
if b=a then ТоЧтоИначеВCase


 
DrPass ©   (2007-07-20 11:28) [1]

idx:= strtoint(copy(a, 5, length(a) - 4));
case idx of...


 
Skyle ©   (2007-07-20 11:32) [2]

1)

const
 S = ".case1.case2.case3.....";

case Pos("." + MyString + ".", S) of
1 :
7 :...
....

2).
 SL := TStringList.Create;
 SL.Add("case1");
......

 case SL.IndexOf(MyString) of
 1 :
 2 :
 .....


 
Vlad Oshin ©   (2007-07-20 11:32) [3]

?
да не,

а="case1";
или
а="kjh";


 
Vlad Oshin ©   (2007-07-20 11:36) [4]


> Skyle ©   (20.07.07 11:32) [2]

ясно. Это думал, это не изящно :)


 
Сергей М. ©   (2007-07-20 11:45) [5]

function StrIdx(Str: String; StrList: array of const): Integer;
begin
 for Result:= 0 to Length(StrList)-1 do
   if StrList[Result]= Str then Exit;
 Result := -1;      
end;

..

case StrIdx("case3", ["case1", "case2", "case3", "case4"]) of
 0: .. case1 ..
 1: .. case2 ..
 2: .. case3 ..
 3: .. case4 ..
else
.. no case ..
end;


 
Skyle ©   (2007-07-20 11:47) [6]


> Vlad Oshin ©   (20.07.07 11:36) [4]
>
> > Skyle ©   (20.07.07 11:32) [2]
>
> ясно. Это думал, это не изящно :)

Хм... Ну а

case MySupaHash(MyString) of
 PreCalcHash1 :
 PreCalcHash2 :
.......


 
Sandman29 ©   (2007-07-20 11:53) [7]

Есть мнение, что использование case следует заменять полиморфизмом.


 
Vlad Oshin ©   (2007-07-20 12:07) [8]

ясно. Брать функцию.
примерно так и подозревал, спасиб

надеялся, что как-то кто-то уже и case приучил к подобному
попроще и побустрее чтоб было.


 
Anatoly Podgoretsky ©   (2007-07-20 13:54) [9]

> Vlad Oshin  (20.07.2007 12:07:08)  [8]

Так case это всего лишь альтернативная форма


if
else if
else


 
Vlad Oshin ©   (2007-07-20 14:29) [10]

Ваша правда ctrl alt с все показал, сразу надо было нажать
тогда по-честному кучу if поставить, быстрее будет.


 
Anatoly Podgoretsky ©   (2007-07-20 14:34) [11]

И главное код понятный.


 
Vendict ©   (2007-07-20 17:31) [12]

Vlad Oshin ©   (20.07.07 12:07) [8]

надеялся, что как-то кто-то уже и case приучил к подобному
попроще и побустрее чтоб было.


есть вариант, но прошлый раз его здесь ругали:
http://andy.jgknet.de/dlang/

DLangExtensions
About

The DLangExtensions adds new language constructs to the ObjectPascal language. This is achieved by using a preprocessor for the IDE and the command line compiler.

Available Language Extensions
case-string-of

The case-string-of extension introduces a case-of that uses strings instead of ordinal-values.Syntax

case string string-expression of
 string-literal/string-constant [, string-literal/string-constant ...]: statement;
 ...
[else
 statements]
end;


 
TUser ©   (2007-07-20 19:36) [13]

const
Names: array [0..11] of shortstring = ...
Schemes: array [0..11] of shortstring = ...

    for i := 0 to length (Names) - 1 do
      if Name = Names[i] then begin
        writeln (StringReplace(Schemes[i],"_",#13,[rfReplaceAll]));
        goto end_of_motif;
        end;
    writeln ("UNCKNOWN MOTIF");
    end_of_motif:


Каждый фрукт, как говорится ...


 
Kolan ©   (2007-07-20 21:44) [14]

> Есть мнение, что использование case следует заменять полиморфизмом.

А как ты тут заменишь? Просто что-то не соображу.

Те надо заменить:

case S of
 "1": Result := 1;
 "2": Result := 2;
end;


?


 
isasa ©   (2007-07-21 00:04) [15]

:)
А в таком варианте
StrToIntDef(...) не прокатит?


 
Sandman29 ©   (2007-07-23 09:36) [16]

Kolan ©   (20.07.07 21:44) [14]

TBaseClass = class
 ...
 procedure DoIt; virtual; abstract;
end;

TCase1 = class(TBaseClass)
 ...
 procedure DoIt; override;
end;

TCase2 = class(TBaseClass)
 ...
 procedure DoIt; override;
end;



Страницы: 1 вся ветка

Текущий архив: 2007.08.19;
Скачать: CL | DM;

Наверх




Память: 0.5 MB
Время: 0.017 c
2-1184997712
pirks
2007-07-21 10:01
2007.08.19
Редактор структурных схем и блок схем


15-1185073431
matt
2007-07-22 07:03
2007.08.19
Рассвет


2-1185277127
kyro
2007-07-24 15:38
2007.08.19
Как сделать сортировку


15-1184888238
Чаво
2007-07-20 03:37
2007.08.19
Как принудительно обновить курсор?


2-1185343359
ZZZZ
2007-07-25 10:02
2007.08.19
Защита программы от дебагеров