Board index » delphi » An Array inside an Open Array

An Array inside an Open Array

hmm,

I am stumped I have a function that I wish to pass in a static array of
values.
eg:
SetPosition(aDataSet, [[ifldNoName, cfldNameName],[ifldNoAddress,
cfldNameAddress]]);

Basically what I want
  function  SetPositions(Dataset : TDataSet; const flds : array of
(longint, string) ) : boolean;
The above gives me syntax error fair enough but what is the correct syntax?

The closest I can get is below where I have to define a record to do the
job..
  function  SetPositions(Dataset : TDataSet; const flds : array of
RFieldLayoutrec) : boolean;
But this is not want I wanted as this means I have to write code like this

FL.ifldNo := ifldNoName;
FL.ifldName := cfldNameName;
SetPosition(aDataSet, [FL,FL2,FL3]);

You see I have to define a RFieldlayoutrec is there away in the structure
to get around this...

:David:.

 

Re:An Array inside an Open Array


Quote
David Szkilnyk wrote:

> hmm,

> I am stumped I have a function that I wish to pass in a static array of
> values.
> eg:
> SetPosition(aDataSet, [[ifldNoName, cfldNameName],[ifldNoAddress,
> cfldNameAddress]]);

> Basically what I want
>   function  SetPositions(Dataset : TDataSet; const flds : array of
> (longint, string) ) : boolean;
> The above gives me syntax error fair enough but what is the correct syntax?

Loopup array of const in the help files.

   function  SetPositions(Dataset : TDataSet; const flds : array of
const):  boolean;

This will let you call it with:
 SetPosition(aDataSet, [ifldNoName, cfldNameName, ifldNoAddress,
cfldNameAddress]);

You can send any type in any order, but in this case you have your own
convention of sending a field number followed by a field contents.

In the SetPositions code you have the check and verify the actual types
and contents of the parameters.

  for i := Low(Flds) to High(Flds) do
    case Flds[i].VType of
      vtInteger : { Handle field number };
      vtString  : { Handle field contenst};
    end;

HTH

--
Hallvard Vassbotn
Falcon R&D
Reuters Norge AS

Re:An Array inside an Open Array


Quote
"David Szkilnyk" <dszki...@lonsdale.com.au> writes:

> hmm,

> I am stumped I have a function that I wish to pass in a static array of
> values.
> eg:
> SetPosition(aDataSet, [[ifldNoName, cfldNameName],[ifldNoAddress,
> cfldNameAddress]]);

> Basically what I want
>   function  SetPositions(Dataset : TDataSet; const flds : array of
> (longint, string) ) : boolean;
> The above gives me syntax error fair enough but what is the correct syntax?

> The closest I can get is below where I have to define a record to do the
> job..
>   function  SetPositions(Dataset : TDataSet; const flds : array of
> RFieldLayoutrec) : boolean;
> But this is not want I wanted as this means I have to write code like this

> FL.ifldNo := ifldNoName;
> FL.ifldName := cfldNameName;
> SetPosition(aDataSet, [FL,FL2,FL3]);

> You see I have to define a RFieldlayoutrec is there away in the structure
> to get around this...

I think you'll have to stay stumped if you don't want to do the record
thing, which is a nicer interface, but a bit of a pain to call.

I can think of another way, but it's a bit fragile and error-prone:

you could write:

FUNCTION  SetPositions (Dataset: TDataSet;
                        CONST FieldNumbers:  ARRAY OF LongInt;
                        CONST FieldNames:    ARRAY OF String):  Boolean;

and call it thus:

SetPosition (aDataSet,
            [ifldNoName, ifldNoAddress],
            [cfldNameName, cfldNameAddress]);

but you have the problem that there is nothing to prevent you from
passing differently-sized arrays for the numbers and the names (e.g. 2
numbers and 3 names).  OK, you can check that at run-time with HIGH,
but you still have the other problem that there is nothing to stop you
from getting the numbers and names in a different order, and it will
be hard to see if you have done that when you come to maintain the
code later.

Without seeing what other data you have at your disposal, it's hard to
suggest a better way.  I think that if there is a fixed relationship
between the names and the numbers that you can define somewhere in a
lookup table in your program, you'd be better just passing either the
numbers OR the names (not both) to your routine, and using the one to
look up the other within the routine.

Hope this helps.

--
-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------

Other Threads