Recent

Author Topic: Newbie Var assignment  (Read 3779 times)

weitsprung

  • Newbie
  • Posts: 2
Newbie Var assignment
« on: April 16, 2015, 01:25:45 am »
How can I make it so that the passed in variable actually gets set in SetVar procedure?


Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure SetVar(avariable: Variant; const avalue: Variant)  ;
  end;

var
  Form1: TForm1;

  b: Boolean;
  s: String;
  i: Integer;


implementation

{$R *.lfm}

procedure TForm1.SetVar(avariable: Variant; const avalue: Variant) ;
begin
  if (avariable <> avalue) then
  begin
    avariable := avalue;
    showmessage('inside: ' + string(avariable));
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  s := 'boat';
  ShowMessage('before: ' + s);
  SetVar(s, 'car');
  ShowMessage('after: ' + s);
end; 

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Newbie Var assignment
« Reply #1 on: April 16, 2015, 01:27:40 am »
Code: [Select]
  //.....
  private
    procedure SetVar(avariable: Variant; const avalue: Variant)  ;
//...
implementation

{$R *.lfm}

procedure TForm1.SetVar(var avariable: Variant; const avalue: Variant) ;
begin //......
 
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

weitsprung

  • Newbie
  • Posts: 2
Re: Newbie Var assignment
« Reply #2 on: April 16, 2015, 02:42:00 am »
that results in

Error: Call by var for arg no. 1 has to match exactly: Got "AnsiString" expected "Variant"

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Newbie Var assignment
« Reply #3 on: April 16, 2015, 02:48:31 am »
that results in

Error: Call by var for arg no. 1 has to match exactly: Got "AnsiString" expected "Variant"
so do what the message says change the "S" declaration to variant.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2667
    • http://www.cdbc.dk
Re: Newbie Var assignment
« Reply #4 on: April 16, 2015, 04:54:13 am »
Hi
quote from taazz:
  //.....
  private
    procedure SetVar(var avariable: Variant; const avalue: Variant)  ;
//...
implementation

{$R *.lfm}

procedure TForm1.SetVar(var avariable: Variant; const avalue: Variant) ;
begin //......

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Newbie Var assignment
« Reply #5 on: April 16, 2015, 08:01:43 am »
Code: [Select]
  //.....
  private
    procedure SetVar(avariable: Variant; const avalue: Variant)  ;
//...
implementation

{$R *.lfm}

procedure TForm1.SetVar(var avariable: Variant; const avalue: Variant) ;
begin //......
 
Taaz means he forgot to adjust the declaration in his example.

Code: [Select]
  //.....
  private
    procedure SetVar(var avariable: Variant; const avalue: Variant)  ;
//...
implementation

{$R *.lfm}

procedure TForm1.SetVar(var avariable: Variant; const avalue: Variant) ;
begin //......
 
  ;)
It's only logical.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Newbie Var assignment
« Reply #6 on: April 16, 2015, 08:15:20 am »
yeah i copy pasted a couple of times between various things I did at the moment and ended up missing a var declaration sorry about that, but my second post was about changing global variable declaration from
Code: [Select]
S:string; to
Code: [Select]
S:Variant;; here is the complete unit with all the changes required to make work.
Code: [Select]

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure SetVar(var aVariable: Variant; const avalue: Variant)  ;
  end;

var
  Form1: TForm1;

  b: Boolean;
  s: Variant;
  i: Integer;


implementation

{$R *.lfm}

procedure TForm1.SetVar(var aVariable: Variant; const avalue: Variant) ;
begin
  if (avariable <> avalue) then
  begin
    avariable := avalue;
    showmessage('inside: ' + string(avariable));
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  s := 'boat';
  ShowMessage('before: ' + s);
  SetVar(s, 'car');
  ShowMessage('after: ' + s);
end; 
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018