Jump to content
Sign in to follow this  
357mag

Why doesn't this work?

Recommended Posts

I've got a program that asks the user to enter the name of a boy, and the name of a girl. Then he clicks on the Add Couple button, and the boy and girl are added to a listbox together. They're going to a dance together.

 

But the compiler will not let me list the variables on individual lines. It will only list my variables on one line. Even my book says you can also write it like this.

 

This does not work:

procedure TFormStringConcatenation.ButtonAddCoupleClick(Sender: TObject);
begin
  Var
    Boy: string;
    Girl: string;
    Couple: string;

    Boy := EditEnterBoy.Text;
    Girl := EditEnterGirl.Text;
    Couple := Boy + ' and ' + Girl;
    ListBoxCouple.Items.Add(Couple);

This however does work:

 

procedure TFormStringConcatenation.ButtonAddCoupleClick(Sender: TObject);
begin
  Var Boy, Girl, Couple: string;

    Boy := EditEnterBoy.Text;
    Girl := EditEnterGirl.Text;
    Couple := Boy + ' and ' + Girl;
    ListBoxCouple.Items.Add(Couple);
end;

 

Share this post


Link to post

You are using inline variables. Try moving the variables declaration before the begin.

procedure TFormStringConcatenation.ButtonAddCoupleClick(Sender: TObject);
Var
  Boy: string;
  Girl: string;
  Couple: string;
begin
  Boy := EditEnterBoy.Text;
  Girl := EditEnterGirl.Text;
  Couple := Boy + ' and ' + Girl;
  ListBoxCouple.Items.Add(Couple);

 

  • Like 1

Share this post


Link to post

Why won't this work with inline vars?
Is it the ListBox.Items.Add that doesn't copy the Couple string?

Share this post


Link to post

With inline vars this works:

Var Boy, Girl, Couple: string;

But this does not:

  Var
    Boy: string;
    Girl: string;
    Couple: string;

 

Share this post


Link to post
procedure TForm1.FormCreate(Sender: TObject);
begin
 Var
    Boy: string;
    Girl: string;
    Couple: string;
end;

For those who doesn't have a modern compiler. Of course inline var is not a block thus it will produce errors:

 

[dcc32 Error] Unit1.pas(29): E2003 Undeclared identifier: 'Girl'
[dcc32 Error] Unit1.pas(29): E2029 '(' expected but ';' found
[dcc32 Error] Unit1.pas(30): E2003 Undeclared identifier: 'Couple'
[dcc32 Fatal Error] Project1.dpr(5): F2063 Could not compile used unit 'Unit1.pas'
Failed

 

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×