Question

is it possible to pass nil as an undeclared constant to the untyped parameter of some function? I have functions like these and I would like to pass some constant to the Data parameter to satisfy the compiler. Internally I'm deciding by the Size parameter. I know I can use Pointer instead of untyped parameter but it's much more comfortable for my case.

Now I'm getting E2250 There is no overloaded version of 'RS232_SendCommand' that can be called with these arguments

function RS232_SendCommand(const Command: Integer): Boolean; overload;
begin
  // is it possible to pass here an undeclared constant like nil in this example?
  Result := RS232_SendCommand(Command, nil, 0);
end;

function RS232_SendCommand(const Command: Integer; const Data; const Size: Integer): Boolean; overload;
begin
  ...
end;

This works, but I would be glad if I could leave the variable declaration.

function RS232_SendCommand(const Command: Integer): Boolean; overload;
var
  Nothing: Pointer;
begin
  Result := RS232_SendCommand(Command, Nothing, 0);
end;

Solution is to use somthing like this.

function RS232_SendCommand(const Command: Integer): Boolean; overload;
begin
  // as the best way looks for me from the accepted answer to use this
  Result := RS232_SendCommand(Command, nil^, 0);

  // or it also possible to pass an empty string constant to the untyped parameter
  // without declaring any variable
  Result := RS232_SendCommand(Command, '', 0);
end;

I'm doing this because some of my commands have a data sent after command transmission.

Thanks for the help

Was it helpful?

Solution

Easy:

RS232_SendCommand(Command, nil^, 0);

You just need to make sure that you don't access the Data parameter from inside RS232_SendCommand, but presumably that's what the 0 size parameter is for.

To my mind this is the best solution because it very explicitly states that you are passing something that cannot be accessed.

OTHER TIPS

No, you can not. not that I knew of

Untyped Parameters

You can omit type specifications when declaring var, const, and out parameters. (Value parameters must be typed.) For example:

procedure TakeAnything(const C);

declares a procedure called TakeAnything that accepts a parameter of any type. When you call such a routine, you cannot pass it a numeral or untyped numeric constant.

From: Parameters (Delphi)

So, perhaps add another overloaded version without the const arg that you can call when Size = 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top