12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>C++</strong> Class Frameworks and the Visual Component Model<br />

underscores proceed the fastcall keyword). It’s not important that<br />

you understand what __fastcall does, but just that every <strong>C++</strong>Builder<br />

function uses this call<strong>in</strong>g convention.<br />

You’ll probably want to make the Code Editor w<strong>in</strong>dow a little wider so you can see all of the<br />

text that is displayed. Before you go on, save the project. Choose File | Save from the ma<strong>in</strong><br />

menu. The first th<strong>in</strong>g you are prompted for is the name of the unit (source file). Type PMEMa<strong>in</strong><br />

and click OK. Next you are prompted for a filename for the project. Type PMETest and press<br />

Enter or click OK. Now on to the good stuff….<br />

Notice that the function is already set up for you and all you have to do is type the code. If<br />

you take a good look at the function, you will see that the function is called ButtonClick, that<br />

it is a member function of the TPMEForm class, that it returns void, and that it takes a po<strong>in</strong>ter<br />

to a TObject called Sender as a parameter. (I’ll talk about the Sender parameter <strong>in</strong> just a bit.)<br />

All that is left to do now is type code that alternately shows or hides the button each time the<br />

button is clicked. We’ll borrow a little code from our earlier discussion of methods. Edit the<br />

ButtonClick function until it looks like this:<br />

void __fastcall TPMEForm::ButtonClick(TObject *Sender)<br />

{<br />

static bool isVisible;<br />

isVisible = !isVisible;<br />

if (isVisible) Memo->Hide();<br />

else Memo->Show();<br />

}<br />

This code sets up a static variable named isVisible.<br />

NEW TERM<br />

A static variable is one that reta<strong>in</strong>s its value between function calls.<br />

Static variables are the exception to the rule regard<strong>in</strong>g un<strong>in</strong>itialized variables—static variables<br />

are <strong>in</strong>itially set to 0. In this case, isVisible is a bool variable, so it is <strong>in</strong>itially set to false.<br />

The second l<strong>in</strong>e of code <strong>in</strong> this function flips the bool variable between true and false by<br />

apply<strong>in</strong>g a logical NOT to the present value of the variable. It works like this: Initially the static<br />

variable is set to false. The first time the function executes, the variable is assigned NOT false,<br />

which is, of course, true. The next time the function executes, the variable is assigned NOT<br />

true, and so on. So each time the function executes, isVisible conta<strong>in</strong>s the opposite value<br />

it had on the previous function call. After that, the if/else pair calls either Show() or Hide()<br />

depend<strong>in</strong>g on the value of isVisible.<br />

<strong>14</strong>9<br />

5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!