Recent

Author Topic: variables behave strange [SOLVED]  (Read 6282 times)

zoiddani

  • New Member
  • *
  • Posts: 29
variables behave strange [SOLVED]
« on: January 04, 2011, 09:45:53 pm »
Today i was trying to make an application which has 2 forms.
the first form uses the 2nd. in the first forms unit i write:
form2.a:=1;
a is declared as an integer in 'public'. however it won't change.

another one:
i have a procedure in the second forms unit, which includes a for cycle. if i declare the cycle's variable outside the procedure (in the 'public' section), then it will give an error. But if i do it inside the procedure, it will run.

however, i have another application, which uses the same things which i wrote above, and that application compiles without errors, and runs fine. What could be the problem?
I use 0.9.28.2 beta
Thank you
« Last Edit: January 07, 2011, 08:08:30 pm by zoiddani »

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: variables behave strange
« Reply #1 on: January 04, 2011, 10:33:29 pm »
Quote
form2.a:=1;

Try to declare "a" variable just below this:

Code: [Select]
var
  Form2: TForm2;

and use it like this:

Code: [Select]
unit2.a := 1; 

Quote
i have a procedure in the second forms unit, which includes a for cycle. if i declare the cycle's variable outside the procedure (in the 'public' section), then it will give an error.

Try to declare it like this:

Code: [Select]
var
  i :integer;

procedure TForm1.Button3Click(Sender: TObject);
begin
  for i := 0 to 1 do
    Application.ProcessMessages;
end;                           

and not on the public section.
« Last Edit: January 04, 2011, 10:38:08 pm by typo »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: variables behave strange
« Reply #2 on: January 05, 2011, 02:26:48 am »
probably a name conflict or incomplete method implementation. Please post complete code.

zoiddani

  • New Member
  • *
  • Posts: 29
Re: variables behave strange
« Reply #3 on: January 06, 2011, 08:11:58 pm »
I've tried what you said, but it won't help.
here is the code, it is the mandelbrot set generator. hope it helps

Code: [Select]
unit rajz1

uses rajz2

procedure TForm1.Button1Click(Sender: TObject);
var err: integer;
begin
  val(edit1.Text,form2.limit,err);  //if i change it to rajz2.form, then lazarus will give an error
  val(edit2.Text,form2.kx,err);
  val(edit3.text,form2.vx,err);
  Form2.Show;
  Form2.rajzolas;
end;

-------

unit rajz2

  TForm2 = class(TForm)
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
  kx,vx,x,y,z,regi_z_x,regi_z_y,uj_z_x,uj_z_y: real;
  szamlalo: longint;
  procedure rajzolas;
    { public declarations }
  end;

var
  Form2: TForm2;
  i,j, limit: integer;

implementation

{ TForm2 }

procedure TForm2.FormCreate(Sender: TObject);
begin
  Width:=Screen.Width;
  Height:=Screen.Height;
  Image1.Width:=Width;
  Image1.height:=Height;
end;

procedure TForm2.rajzolas;
begin;
//limit:=200; <-- if i comment these out, it wont work, bcos the variables dont get the values from
//the other form
//kx:=-1.5;
//vx:=0.5;

for i:=0 to width do
begin;
x:=((vx-kx)/width*i)+kx;
Image1.Refresh;
  for j:= -height div 2 to height div 2 do
  begin;
  y:=((vx-kx)/width*(j));
  regi_z_x:=0;
  regi_z_y:=0;
  counter:=0;
    repeat
      uj_z_x:=regi_z_x*regi_z_x-regi_z_y*regi_z_y+x;

      uj_z_y:=2*regi_z_x*regi_z_y+y;

      regi_z_x:=uj_z_x;

      regi_z_y:=uj_z_y;

      z:=sqrt(uj_z_x*uj_z_x+uj_z_y*uj_z_y);
      counter:=counter+1;

    until (counter>limit) or (z>2);

    Image1.Canvas.pixels[i,j+height div 2]:=counter;
 
  end;
end;
end;
« Last Edit: January 06, 2011, 08:16:10 pm by zoiddani »

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: variables behave strange
« Reply #4 on: January 07, 2011, 03:07:57 am »
Code: [Select]
[quote author=zoiddani link=topic=11669.msg58792#msg58792 date=1294341118]
I've tried what you said, but it won't help.
here is the code, it is the mandelbrot set generator. hope it helps

[code]unit rajz1

uses rajz2

procedure TForm1.Button1Click(Sender: TObject);
var err: integer;
begin
  val(edit1.Text,form2.limit,err);  //if i change it to rajz2.form, then lazarus will give an error
  val(edit2.Text,form2.kx,err);
  val(edit3.text,form2.vx,err);
  Form2.Show;
  Form2.rajzolas;
end;
[/quote]
limit needs to belong to form2 if accessed this way.  

Code: [Select]
unit rajz2

  TForm2 = class(TForm)
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
  kx,vx,x,y,z,regi_z_x,regi_z_y,uj_z_x,uj_z_y: real;
  szamlalo: longint;
  procedure rajzolas;
    { public declarations }
  end;

var
  Form2: TForm2;
  i,j, limit: integer;  <== these belong to the unit
...
but it belongs to the unit if placed here. unit <> form!  
just move it to the public part of form2 like you did with the reals and it should work.[/code]
« Last Edit: January 07, 2011, 03:11:36 am by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: variables behave strange
« Reply #5 on: January 07, 2011, 08:45:53 am »
Code: [Select]
var
  Form2: TForm2;
  i,j, limit: integer;

implementation

Or you can simply swap to the other side of implementation where they become private to that unit use only
Code: [Select]
var
  Form2: TForm2;

implementation

var
  i,j, limit: integer;

zoiddani

  • New Member
  • *
  • Posts: 29
Re: variables behave strange
« Reply #6 on: January 07, 2011, 08:02:09 pm »
Ok..
I've rewritten the code, declaring the variables as you said. Now it works like a charm. Thanks! :)

 

TinyPortal © 2005-2018