Recent

Author Topic: A simple program with a weird bug.  (Read 3313 times)

zoimutante

  • New Member
  • *
  • Posts: 34
A simple program with a weird bug.
« on: August 08, 2015, 11:06:31 pm »
I have a simple program, with twos tedits, edit1 and edit2 ok ?
a var declared as string
and a simple code on edit1

procedure TForm1.Edit1Change(Sender: TObject);
begin
  name:=(Edit1.Text);
  Edit2.Text:=name;
end;   

I wish to get the text in tedit1 and pass it to a variable  name and change the text of the edit2 to the content in the variable name ok ?

the problem is that if a use a space on the text in edit1 I get a runtime error



« Last Edit: August 08, 2015, 11:27:46 pm by zoimutante »

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: A simple program with a weird bug.
« Reply #1 on: August 08, 2015, 11:49:59 pm »
hello,
don't use name as variable , in your code , name is the property name of the TForm1 component. 
For example , you can do :
Code: [Select]
procedure TForm1.Edit1Change(Sender: TObject);
var myname : string;
begin
   myname:=(Edit1.Text);
  Edit2.Text:= myname;
end;
and why you use a variable ? you use the variable in an another place in your program ?you can do :
Code: [Select]
procedure TForm1.Edit1Change(Sender: TObject);
var myname : string;
begin
  Edit2.Text:= Edit1.Text;
end;
« Last Edit: August 08, 2015, 11:52:16 pm by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: A simple program with a weird bug.
« Reply #2 on: August 08, 2015, 11:53:24 pm »
LOL, You are using the "Name" as the name of the variable which is also a property of the Form. namely the Name of the Form.
Try to rename the Variable to e.G: MyName, EnteredName or set the scope:
e.G:
Code: [Select]
<unitname>.name := ...
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

zoimutante

  • New Member
  • *
  • Posts: 34
Re: A simple program with a weird bug.
« Reply #3 on: August 09, 2015, 12:04:56 am »
Thank you both Jurassic Pork and JC99

A guy in the irc channel named x86iac point me the exact same mistake, thank you 3 :)

FPnewbie

  • Newbie
  • Posts: 6
Re: A simple program with a weird bug.
« Reply #4 on: August 12, 2015, 09:30:00 pm »
I am trying to set a var mystring:=Edit1Text.
When I try to use the following:

procedure TForm1.Edit1Text (Sender: TObject);

var mystring: string;

   mystring:=Edit1text;


I get the procedure line hilited and the error "Method identifier expected". What am I not understanding?

derek.john.evans

  • Guest
Re: A simple program with a weird bug.
« Reply #5 on: August 12, 2015, 09:42:05 pm »
Edit1Text is a method. Not a string. You need:
Code: [Select]
mystring := Edit1.Text
Note: The dot.

jc99

  • Hero Member
  • *****
  • Posts: 553
    • My private Site
Re: A simple program with a weird bug.
« Reply #6 on: August 12, 2015, 09:42:22 pm »
First read  http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F
Then put you Code in [ Code ]-tags
So it looks like:
Code: [Select]
procedure TForm1.Edit1Text (Sender: TObject);

var mystring: string;

   mystring:=Edit1text;
besides begin and end is missing, you named your method Edit1Text.
Then you try to assign this to a string, which is not possible.

Code: [Select]
procedure TForm1.Edit1Text (Sender: TObject);
var mystring: string;
begin
   mystring:=Edit1.text; // The dot was missing (maybe)
   //...
end;
Then you have valid code ...
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

 

TinyPortal © 2005-2018