Delphi 10.1, function that only accepts an integer interval in the variable

2

It seems easy, but I can not. I want to be able to define a variable type when creating a function or procedure, but only accept integer numbers between 1 and 4 for example during function / procedure call. These numbers can already be used on it.

something like:

function Algo(TAlgo: array [1...4] of integer);

Where I can not even compile if I call:

Something (5); // return error, neither accept compile
Something (1); // ok
Something (2); // ok
Something (3); // ok
Something (4); // ok


I was able to get close, but I'm trying to simplify the code below, as shown above. So far I've achieved something similar by creating a Type as below:

I created a Type :

    TQtd = (xUma, xDuas, xTres, xQuatro);

And inside the function, a var and a case:

var
  iQtd:Integer;
...
  case Precision of
    xUma: iQtd := 1;
    xDuas: iQtd := 2;
    xTres: iQtd := 3;
    xQuatro: iQtd := 4;
  end;

So, I use the iQtd variable and get what I need, calling the function this way:

functionTal(xTres);

Is there any way I can create, by specifying integer, accepting only 1 to 4?

    
asked by anonymous 28.06.2018 / 02:02

1 answer

7

You can create a type using an integer range, eg:

Type
  TAteQuatro = 1..4;

  procedure Teste(valor: TAteQuatro);
  begin
    Writeln(valor);
  end;
begin
  Teste(4); //Compila
  Teste(5); //[dcc32 Error] Rangezera.dpr(20): E1012 Constant expression violates subrange bounds
end.
    
28.06.2018 / 16:09