Board index » delphi » How to solve - Incompatible types: 'Array' and 'Dynamic array'

How to solve - Incompatible types: 'Array' and 'Dynamic array'

I have a TNMUDP socket on a form that will receive varable length packets.
The ReadBuffer method of the TNMUDP component expects an array of Char as
a parameter but I want to size the array to match the number of bytes
received.
How can I do this so the compiler won't complain.

procedure TUDPSocket.TNMUDP1DataReceived(Sender: TComponent;
? NumberBytes: Integer; FromIP: String; Port: Integer);
var
? Buffer: array of Char;
?
begin
? SetLength(Buffer,NumberBytes);
? TNMUDP1.ReadBuffer(Buffer, NumberBytes);
end;
?
end.

Thanks in advance.
Ullas

 

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


Try :  TNMUDP1.ReadBuffer(Buffer[0], NumberBytes);

or:
         TNMUDP1.ReadBuffer(@Buffer[0], NumberBytes);

You might need to use a bit more typecasting

Personally I would make Buffer a String

On Mon, 19 May 2003 13:20:37 GMT, "Ullas" <ul...@linknet.com.au>
wrote:

Quote
>I have a TNMUDP socket on a form that will receive varable length packets.
>The ReadBuffer method of the TNMUDP component expects an array of Char as
>a parameter but I want to size the array to match the number of bytes
>received.
>How can I do this so the compiler won't complain.

>procedure TUDPSocket.TNMUDP1DataReceived(Sender: TComponent;
>? NumberBytes: Integer; FromIP: String; Port: Integer);
>var
>? Buffer: array of Char;
>?
>begin
>? SetLength(Buffer,NumberBytes);
>? TNMUDP1.ReadBuffer(Buffer, NumberBytes);
>end;
>?
>end.

>Thanks in advance.
>Ullas

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


TNMUDP1.ReadBuffer(Buffer[0], NumberBytes); works just fine.

Thank you.

I am new to Delphi - coming from VB. If I was to make Buffer a
string how would I typecast it so ReadBuffer would see it as an
array of char?

Quote
On Mon, 19 May 2003 13:27:18 +0000, J French wrote:
> Try :  TNMUDP1.ReadBuffer(Buffer[0], NumberBytes);

> or:
>          TNMUDP1.ReadBuffer(@Buffer[0], NumberBytes);

> You might need to use a bit more typecasting

> Personally I would make Buffer a String

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


On Mon, 19 May 2003 13:40:18 GMT, "Ullas" <ul...@linknet.com.au>
wrote:

Quote
>TNMUDP1.ReadBuffer(Buffer[0], NumberBytes); works just fine.

>Thank you.

>I am new to Delphi - coming from VB. If I was to make Buffer a
>string how would I typecast it so ReadBuffer would see it as an
>array of char?

I think that I gave you bad advice
- if you are dealing with 'open arrays' the High() of the Array is
stored somewhere

Using :  S[1] is great when a PChar is expected

but there is something I have not quite got a handle on about open
fixed length arrays

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


I suggest that you're trying to over-engineer the problem.  Allocate the
buffer to match the largest size your program will accept .. plus about
eight bytes for slop.  Use "AllocMem" or "FillChar" to explicitly set the
whole thing to zeroes ... and do that every time.

[Those extra eight bytes, and all those known-zeroes, can avoid a hornet's
nest of nasss-s-s-s--sty problems.]

When you've got a packet, set the array-size and use a <!>ed 'for' loop to
fill it.

Unless you have an /incredible/ amount of traffic coming in every
millisecond, the CPU will be grateful that you gave it something to do, no
matter how trivial it might be.

I always advocate going for the simplest solution, not the cleverest.  If at
some time in the future you slap a performance-probe on the program and
"this particular harbor" (yeah, I was groovin' to Jimmy Buffett last night)
shows up as a cherry-red hot spot, /then/ and /only/ then do you reimplement
it.

The true 'hot spots' in a program are almost never where you expect them to
be.

Quote
Ullas wrote:
> I have a TNMUDP socket on a form that will receive varable length packets.
> The ReadBuffer method of the TNMUDP component expects an array of Char as
> a parameter but I want to size the array to match the number of bytes
> received.
> How can I do this so the compiler won't complain.

> procedure TUDPSocket.TNMUDP1DataReceived(Sender: TComponent;
> NumberBytes: Integer; FromIP: String; Port: Integer);
> var
> Buffer: array of Char;

> begin
> SetLength(Buffer,NumberBytes);
> TNMUDP1.ReadBuffer(Buffer, NumberBytes);
> end;

----------------------------------
Fast automatic table repair at a click of a mouse!
http://www.sundialservices.com/products/chimneysweep

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


On Mon, 19 May 2003 09:12:13 -0700, Sundial Services

Quote
<info_...@sundialservices.com> wrote:
>I suggest that you're trying to over-engineer the problem.  Allocate the
>buffer to match the largest size your program will accept .. plus about
>eight bytes for slop.  Use "AllocMem" or "FillChar" to explicitly set the
>whole thing to zeroes ... and do that every time.

I am afraid that I was responsible for this

- I was daft enough to _not_ realize that he is dealing with a 3rd
party component, that utilizes a fixed length (but rigorously checked)
buffer and allows partial fills.

I would also add to your recommendation :-

  'turn range checking on - and keep it on'

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


You could also use a pointer - for example:

type
  TCharArray = array[1..10000] of char;
  PCharArray = ^TCharArray;
var
  buffer: PCharArray;
begin
  GetMem(buffer, NumberBytes);
  TNMUDP1.ReadBuffer(buffer^, NumberBytes);
  FreeMem(buffer);
end;

--
Regards

John Bester
mailto:john.bes...@adept.co.za

Re:How to solve - Incompatible types: 'Array' and 'Dynamic array'


On Tue, 20 May 2003 14:33:53 +0200, "John Bester" <no...@nowhere.com>
wrote:

Quote
>You could also use a pointer - for example:

Not if the thing is 3rd party and has Bounds Checking turned on
(which is sensible)
Quote

>type
>  TCharArray = array[1..10000] of char;
>  PCharArray = ^TCharArray;
>var
>  buffer: PCharArray;
>begin
>  GetMem(buffer, NumberBytes);
>  TNMUDP1.ReadBuffer(buffer^, NumberBytes);
>  FreeMem(buffer);
>end;

>--
>Regards

>John Bester
>mailto:john.bes...@adept.co.za

Other Threads