Err, Jason ...
Your result will point to deep space
Because 'Data' is defined within the function, everything (including Data)
will be out of scope

This is a classic 'C'-style bug
Having your proc as a procedure with a var pointer param would avoid this
 
I try, whenever possible, to steer clear of pointers & untyped vars declared
as params or function results.
 
How about this?
 
type
  PType1 = ^TType1;
  PType2 = ^TType2;
procedure LoadTemplate(const AData: string; var ARec1:PType1; var ARec2:
PType2);
(Populate the rec you want & set the other to nil)
 
 
or this
 
type
  TBaseRecObj = class
 ...
end;
  TObj1 = class(TBaseRecObj) ...
  TObj2 = class(TBaseRecObj) ...
 
procedure LoadTemplate(const AData: string; var ARecObj:TBaseRecObj);
...
LoadTemplate(sData, RecObj); // proc creates TObj1 or TObj2
try
  if RecObj is TObj1 then ...
  if RecObj is TObj2 then ...
finally
  FreeAndNil(RecObj)
end;
 
 
 

Sorry, that was just an example, no the types are completely different, I
should have made this clear.

 

I'm not too good with pointers, but would the code below be correct for the
implementation.

 

function LoadTemplate(Path: String): Pointer;

var

  Stream: TFileStream;

  Data: array[0..43450] of char;

begin

  Stream := nil;

  FillChar(Data, Sizeof(Data), #00);

 

  Stream := TFileStream.Create(Path, fmOpenRead or fmShareDenyWrite);

  Stream.Position := 0;

  Stream.Read(Data, Stream.Size);

 

  ClearStyle(Result);

  ShowMessage(strCantReadDat + MyStylesPath + strExists);

 

  Result := @Data;

  Stream.Free;

end;

 

 

and use it like:

 

MyTemplate := TTemplate(LoadTemplate(Path)^);

 

As I said this may not be correct, but Ive never really used pointers
before.

 

Jason

 

 

-----Original Message-----
From: Andreas Toth [mailto:[EMAIL PROTECTED]] 
Sent: Monday, 14 October 2002 11:14 a.m.
To: Multiple recipients of list delphi
Subject: RE: [DUG]: Load different types from one procedure?

 

I don't like the sound of using the path as an indicator of how the data is
to be interpreted. A better solution would be to pass in a third parameter
which defines the way the data is to be interpreted, i.e., the record to
use.

 

By the way, depending on how much control you have over the data, I would
really try to do away with one of the records, since. after all, they
describe the same same information, just in a different order. Is this
really necessary?

 

 

-Andreas

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Jason Coley
Sent: Monday, 14 October 2002 10:29
To: Multiple recipients of list delphi
Subject: [DUG]: Load different types from one procedure?

Is there a way I can load different record type objects using one procedure?

 

I have 

 

TType1 = record

            intone: integer;

            strone: array[1..49] of char;

end;

 

TType2 = record

            strtwo: array[1..49] of char;

            Inttwo: integer;

end;

 

These types are stored as files on my system, but I want to have one
procedure/function that can load either type by simply passing the path, can
you do this.

 

I have something like 

 

Procedure LoadTemplate(Path: String; var Data: array of char);

 

But this doesn't work as the type and array are not compatible.

 

I think this could be a Monday morning thing, but I have had two
coffee's!!!!

 

Jason

<<attachment: winmail.dat>>

Reply via email to