Reputation: 2593
i have an issue where, when i go to add more "names" to the if statements. Its hard for me to see if there all ready on there. Thus is there a more clean way to write this Where i can easly see what names are all read there?
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;
Upvotes: 3
Views: 209
Reputation: 2416
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
Upvotes: 1
Reputation: 612964
You could create a dictionary, TDictionary<string, Integer>
, and store it in a global variable. Load it up with the name to width mapping at initialization time. And then your function becomes a one-liner.
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;
Upvotes: 10
Reputation: 19346
Yes, don't use an if
statement but an array and a loop:
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;
Additional advantage: you don't need to keep the names which return the same values together but can sort the list alphabetically.
Upvotes: 5