Link to home
Start Free TrialLog in
Avatar of LostInSpace2
LostInSpace2

asked on

function questions

Hi Experts

I`m attempting to learn functions but I seem to be lost. I wanted a function that takes the integer entry in a editbox and multiplies that value by 2.
One of the error messages states[Error] Unit1MainCab32.pas(77): Undeclared identifier: 'edCabQty'

Thanks for the help

function PcOneCount (Pc: Integer) : Integer;
begin
 Pc : = StrToInt(edCabQty.Text);
  PcOneCount := Pc * 2;
end;
Avatar of Lukasz Lach
Lukasz Lach

add
function PcOneCount (Pc: Integer) : Integer;
to public in your form cladd and should be in implementation:

function TForm1.PcOneCount (Pc: Integer) : Integer;
begin
  Pc : = StrToInt(edCabQty.Text);
  Result := Pc * 2;
end;


btw what is PC variable for in the funcion? ;-)
that will work better:

function TForm1.PcOneCount: Integer;
begin
  Result : = StrToInt(edCabQty.Text)*2;
end;
ASKER CERTIFIED SOLUTION
Avatar of loop_until
loop_until

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
you must also qualify the object where
edCabQty.Text resides, if your function is not
a method of this object

guessing your object is form1 use

form1.edCabQty.Text

instead of

edCabQty.Text

meikl ;-)
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
  Function Multiply(Number: Integer): Integer;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function Multiply(Number: Integer): Integer;
begin
Form1.Edit1.Text := IntToStr(StrToInt(Form1.Edit1.Text)*2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Multiply(2);
end;

end.


All you have to do to call the function is Multiply(someinteger), so you would type Multiply(2); to multiply the contents of Edit1.text by 2.  You must put the function in the Uses part of the program as shown above.

To use this just create a new application, add an Edit box and a button, and your good to go.

-Josh-