Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Issue in Record type with Variant Part


This question is answered.


Permlink Replies: 2 - Last Post: Dec 22, 2014 1:31 AM Last Post By: Jitendra Gouda
Jitendra Gouda

Posts: 11
Registered: 3/2/13
Issue in Record type with Variant Part  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 21, 2014 9:25 PM
Hi,

We have declared a Record with Variant part as follow in Delphi XE3.
var
sTemp: Record
case Integer of
0: (s: SecureBuf);
1: (x: array [TSecure] of char);
end;

But when I assign values of x array items and try to get the values then it results correct.
But when I try to get value of S then it shows null value.

That same thing I have checked in Delphi 7 it works fine.
It shows correct value for both X and S.

Following is the detail code of unit that shows the issue...

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, frxClass, Vcl.StdCtrls;
type
CryptBuf = Array[0..4096] of Char;

TSecure = (slNone, slInvView, slInvAdd, slInvDel, slInvRpView, slInvRpAdd, slInvRpDel,
slCommView, slCommAdd, slCommDel, slGlRpts, slChart, slProof,
slSettleNone, slSettleView, slSettleFull, slCorpNone, slCorpView,
slCorpFull, slAgtCommNone, slAgtCommView, slAgtCommFull, slJENone,
slJEView, slJEFull, slIdNone, slIdView, slIdFull, slSysHigh, slSysLow,
slSysNone, slBankDep, slPostDep, slAdjust, slExpPay, slBankReco,
slManAutoReco, slChangeDate, slPostInvoice, slSeeCash, slChecks,
slSalesAnal, slApReports, slInterface, slGrpRpts, slGrpAdjust, slGrpPaxView,
slGrpPaxAdd, slGrpPaxDel, slPrintChecks, slVoidDep, slImpCC, slMaskCC,
slEmpSec, slReprintChecks);

TSecurityLevel = Set of TSecure;
SecureBuf = String[54];

type
TForm1 = class(TForm)
Button5: TButton;
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

*{$R .dfm}

//Procedure that shows the issue//
procedure TForm1.Button5Click(Sender: TObject);
var
sTemp: Record
case Integer of
0: (s: SecureBuf);
1: (x: array [TSecure] of char);
end;
sStr: String;
begin
sTemp.x[slNone] := chr(ord(High(TSecure)));
sTemp.x[slInvView] := 'Y';
sTemp.x[slInvAdd] := 'Y';
sTemp.x[slInvDel] := 'Y';
sTemp.x[slInvRpView] := 'Y';
sTemp.x[slInvRpAdd] := 'Y';
sTemp.x[slInvRpDel] := 'Y';
sTemp.x[slCommView] := 'Y';
sTemp.x[slCommAdd] := 'Y';
sTemp.x[slCommDel] := 'Y';
sTemp.x[slGlRpts] := 'Y';
sTemp.x[slChart] := 'Y';
sTemp.x[slProof] := 'Y';
sTemp.x[slSettleNone] := 'Y';
sTemp.x[slSettleView] := 'Y';
sTemp.x[slSettleFull] := 'Y';
sTemp.x[slCorpNone] := 'Y';
sTemp.x[slCorpView] := 'Y';
sTemp.x[slCorpFull] := 'Y';
sTemp.x[slAgtCommNone] := 'Y';
sTemp.x[slAgtCommView] := 'Y';
sTemp.x[slAgtCommFull] := 'Y';
sTemp.x[slJENone] := 'Y';
sTemp.x[slJEView] := 'Y';
sTemp.x[slJEFull] := 'Y';
sTemp.x[slIdNone] := 'Y';
sTemp.x[slIdView] := 'Y';
sTemp.x[slIdFull] := 'Y';
sTemp.x[slSysHigh] := 'Y';
sTemp.x[slSysLow] := 'Y';
sTemp.x[slSysNone] := 'Y';
sTemp.x[slBankDep] := 'Y';
sTemp.x[slPostDep] := 'Y';
sTemp.x[slAdjust] := 'Y';
sTemp.x[slExpPay] := 'Y';
sTemp.x[slBankReco] := 'Y';
sTemp.x[slManAutoReco] := 'Y';
sTemp.x[slChangeDate] := 'Y';
sTemp.x[slPostInvoice] := 'Y';
sTemp.x[slSeeCash] := 'Y';
sTemp.x[slChecks] := 'Y';
sTemp.x[slSalesAnal] := 'Y';
sTemp.x[slApReports] := 'Y';
sTemp.x[slInterface] := 'Y';
sTemp.x[slGrpRpts] := 'Y';
sTemp.x[slGrpAdjust] := 'Y';
sTemp.x[slGrpPaxView] := 'Y';
sTemp.x[slGrpPaxAdd] := 'Y';
sTemp.x[slGrpPaxDel] := 'Y';
sTemp.x[slPrintChecks] := 'Y';
sTemp.x[slVoidDep] := 'Y';
sTemp.x[slImpCC] := 'Y';
sTemp.x[slMaskCC] := 'N';
sTemp.x[slEmpSec] := 'Y';
sTemp.x[slReprintChecks] := 'Y';
sStr := sTemp.s;
ShowMessage(sTemp.s);
ShowMessage(sTemp.x);
end;
end.

So could any one please help on this issue.
Thanks,

I
--
MR JKG

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Issue in Record type with Variant Part
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 21, 2014 10:36 PM   in response to: Jitendra Gouda in response to: Jitendra Gouda
Jitendra wrote:

We have declared a Record with Variant part as follow in Delphi XE3.
<snip>
That same thing I have checked in Delphi 7 it works fine. It shows
correct value for both X and S.

Char was an alias for AnsiChar in D7, but is now an alias for WideChar in
D2009+. SecureBuf is a ShortString, which is still Ansi-based, not Unicode.
You are getting nulls in your ShortString because Char is now 16bit where
the high 8 bits are being set to zeros. You need to change your record to
use AnsiChar instead of Char to maintain your original logic:

var
  sTemp: Record
    case Integer of
      0: (s: SecureBuf);
      1: (x: array [TSecure] of AnsiChar);
  end;


--
Remy Lebeau (TeamB)
Jitendra Gouda

Posts: 11
Registered: 3/2/13
Re: Issue in Record type with Variant Part  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 22, 2014 1:31 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Jitendra wrote:

We have declared a Record with Variant part as follow in Delphi XE3.
<snip>
That same thing I have checked in Delphi 7 it works fine. It shows
correct value for both X and S.

Char was an alias for AnsiChar in D7, but is now an alias for WideChar in
D2009+. SecureBuf is a ShortString, which is still Ansi-based, not Unicode.
You are getting nulls in your ShortString because Char is now 16bit where
the high 8 bits are being set to zeros. You need to change your record to
use AnsiChar instead of Char to maintain your original logic:

var
  sTemp: Record
    case Integer of
      0: (s: SecureBuf);
      1: (x: array [TSecure] of AnsiChar);
  end;


--
Remy Lebeau (TeamB)


Thanks, It worked....

--
MR JKG

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02