3

我有一个问题,当我向 if 语句添加更多“名称”时。我很难看到那里是否都准备好了。因此,有没有更干净的方法来写这个我可以很容易地看到那里读到了哪些名字?

    function TfDB.GetW(name: string) :integer;
  begin
     result := 0;
    if (name = 'Destacker') or (name='Router') or (name = 'Turn Table') Then
        result := 57;
    if (name = 'Laser Marker') then
        result := 66;
    if (name = 'SP28')OR(name='Chamber') OR (name = 'CM402') OR (name = 'SP60') then
        result := 65;
    if (name = 'Conveyor') OR (name = 'Slide Gate') OR (name = 'Washer') then
        result := 51;
    if (name = 'BTU') OR (name = 'Furukawa') OR (name = 'Radial') OR (name = 'HDP') or (name = 'MSR') OR (name = 'Koki') Or (name = 'MSF') then
        result := 98;
    if (name = 'Buffer')OR (name = 'Reclaimer') OR (name = 'ECO Roller') then
        result := 49;
    if (name = 'Inverter') or (name = 'CNC') then
        result := 42;
    if (name = '3-D Check Station') or (name='Screw Machine') or (name='VT-Win') or(name='Component Viewer') then
        result := 58;
    if (name = 'AOI Panel') or (name='AirBlow') then
        result := 42;
    if (name='Mag Loader') or (name='Soltec') then
        result := 73;
    if (name='Tester') then
        result := 33;
    if (name='LoadBox') then
        result := 17;
    if (name = 'DeltaWave') then
        result := 89;
    if (name = 'ScrewFeeder') then
        result := 25;
    if (name='Pump') then
        result := 33;

    //if result is 0 show message error.

  end;
4

3 回答 3

10

您可以创建一个字典,TDictionary<string, Integer>并将其存储在一个全局变量中。在初始化时使用名称到宽度的映射加载它。然后你的函数就变成了单行。

var
  WidthMapping: TDictionary<string, Integer>;
....
function TfDB.GetW(name: string) :integer;   
begin
  if not WidthMapping.TryGetValue(name, Result) then
    ... handle error condition
end;
....
initialization
  WidthMapping := TDictionary<string, Integer>.Create;
  WidthMapping.Add('Destacker', 57);
  WidthMapping.Add('Router', 57);
  ... etc.
....
finalization
  WidthMapping.Free;
于 2013-02-06T07:46:01.380 回答
5

是的,不要使用if语句,而是使用数组和循环:

const
  NAME_RESULT: array [1..2] of record
    Name: string;
    Value: Integer;
  end = (
    (Name: 'whatever'; Value: 57)
  , (Name: 'Something else'; Value: 57)
  );
var
  i: Integer;
begin
  Result := 0; // or whatever you need your default to be
  for i := Low(NAME_RESULT) to High(NAME_RESULT) do
    if SameText(name, NAME_RESULT[i].Name) then
    begin
      Result := NAME_RESULT[i].Value;
      Break;
    end;
end;

附加优势:您不需要将返回相同值的名称保留在一起,但可以按字母顺序对列表进行排序。

于 2013-02-06T07:46:13.690 回答
1

Create an array (or dynamic array if you want to further add more names without any concerns of the array dimensions) of strings that contains all the names you want to test for (here I assumed fixed size string array):

    var test = array [1..50] of string;
    a[1]:='Destacker';
    a[2]:='Router';

etc.

In your test routine you may use the case keyword like this:

    function TfDB.GetW(index: integer) :integer
    begin
     result:=0;
     case index of
      1,2,3: result:=57;
      4: result:=66
     end
    end;

I think it's easier this way

于 2013-02-06T08:14:21.453 回答