Programming with C#Builder

For Windows programmers, Microsoft’s .NET framework defines the shape of things to come. Encapsulating a large library of classes and development tools, .NET will progressively replace older class libraries and compilers. The C# language was designed to be the ‘native language’ of .NET and a competence in C# looks set to be an invaluable skill.
Until now, the main barrier to anyone wishing to learn C# has been the cost. A full copy of Microsoft’s Visual Studio .NET or Borland’s C#Builder Professional costs hundreds of pounds, but this month we’ve demolished the cost barrier. The free copy of C#Builder Personal Edition on the SuperDisc of PC Plus issue 211 provides you with a powerful C# development environment, complete with visual design tools, a tightly integrated editor and a debugger. Note that you’ll need to install the .NET framework separately and enable your copy of C#Builder by registration, as explained on the PCP 211 disc.
Creating a project
In C#Builder, all your code is arranged in the form of projects. You can arrange multiple projects into a ‘project group’, which is the Borland equivalent of what Microsoft calls a ‘solution’. To start a new project, select ‘New’, ‘C# Application’ from the File menu. In the dialog box, give the project a name, such as TestApp, pick a location in which to store the source code and click ‘OK’. You will now see a blank form in the central design area of the screen.
In C#, as in Java, the visual design of a form is defined in program code. To view the code, press [F12] to toggle between the form designer and the code editor. Towards the bottom of the unit, you’ll see the text, ‘Windows Form Designer generated code’. Note the small ‘+’ sign in the margin to the left. This indicates that some code has been hidden. Simply click the plus sign to expand the hidden area. This is where C#Builder keeps the code that defines your form. There isn’t much here at the moment but you’ll see that it grows in complexity as you add controls to the form.
Press [F12] to switch back to the form designer. Find the Button control in the Tool palette at the bottom right of your workspace. Click this, then click the form to drop a button onto it. Next, drop a text box onto the form in the same way. If you switch back to the editor, you’ll see that the form design code has now expanded to create and position the controls on the form. Needless to say, you shouldn’t edit this auto-generated code. Click the minus sign alongside the Windows Form Designer region to collapse it again.
Adjusting properties
In common with most other visual programming systems, C#Builder provides a list of properties which enable you to make changes to the appearance and behaviour of components. By default, the properties are grouped by category in the Object Inspector to the left of your workspace. If you prefer to view them alphabetically, right-click the Object Inspector and select ‘Arrange’, ‘By Name’.
Choose textBox1 on your form and use the Object Inspector to change its Name property to outputBox. Select its Text property and delete the text. The box on your form will now be blank. Select button1 and change its Text property to ‘Click Me!’. Finally, click the background of the form to select it and change its Text property to My C# App. This text will appear in the form’s caption bar.
If you’re familiar with another visual design system, you may be surprised to find that the Text property refers to both the editable text of a text box and the fixed text of the button and form. This is an example of the consistency of naming that characterises much of C# and .NET.
Adding event-handlers
Let’s add some code. With the form designer visible, click the Events tab in the Object Inspector. Select the button on the form. Double- click the empty cell next to the Click event. This has the effect of creating a blank event-handler named button1_Click() and the cursor will be placed inside this method in the editor. Between the braces of this method, enter the name of the text box, outputBox.
Make sure that you use the appropriate case for each of the characters in this identifier. Unlike Delphi and Visual Basic, the C# language is case sensitive, so a component or variable named ‘Outputbox’ is considered different from one named ‘outputBox’. Place a full stop at the end of the name. The code in this method should now look like this:
private void button1_Click(object sender, System.EventArgs e)
{
outputBox.
}
Assuming that you’ve misspelled outputBox, a drop-down list should appear when you add the full stop at the end. If you click the mouse in the editor, this list will disappear. To make it pop up again, just place your cursor after the full stop and press the space bar. This list contains the names of ‘members’ – that is, the properties, methods and events – of the specific object. We want to use the Text property so click ‘Text’ in the list, then complete the code lines like this:
outputBox.Text = “Hello world”;
To test that this program works, run it by pressing [F9]. All being well, the text ‘Hello world’ will appear in the text box when you click the button. When you’ve finished, close your application and return to the form editor.
Editing code
Let’s try coding something that’s a little more useful. We’ll create a simple miles-to-kilometres conversion calculator. The user will enter a distance in miles in one text box and our application will display the kilometre equivalent in another. Drop a second text box onto the form above the existing one, delete its text and name it inputBox. In the code editor, find the button1_Click() method, delete the existing code and replace it with the following:
outputBox.Text = Convert.ToString( Convert.ToDouble(inputBox.Text)*1.60934);
Here, Convert is a .NET Class which provides the ToString() and ToDouble() methods to translate between strings and floating point numbers. Try running the program and entering a numerical value into inputBox to test that it is converted when you click the button.
Things to note when you edit the code include the automatic highlighting of matching braces when you place your cursor over one of a pair, the list of valid arguments that drops down when you enter an opening brace (for example, after ToDouble) and the syntax colouring of keywords, numerical types and strings. You can tailor these and other editor features to suit your personal tastes by selecting Options from the Tools menu.
Now that you’ve programmed this simple calculator, you’ve already learnt the fundamental techniques of application development with C#Builder. If you know another programming language, you may be able to pick up many of the skills needed for more advanced C# programming with the assistance of the built-in help. We’ll be exploring C#Builder in more depth next month.
C#Builder provides a sophisticated source code debugger which helps to find, evaluate and fix programming errors. For simple debugging, it may be sufficient to place a breakpoint on a line of code so the program pauses when that piece of code executes. To place a breakpoint, double-click in the grey margin area to the left of the editor window. A red circle in the margin shows that a breakpoint has been set.
When that breakpoint is met, you can trace through subsequent lines of code by pressing [F7] to trace into all lines of code or [F8] to step over lines of code that are outside the current method. If the C# code is unavailable, a disassembly window opens so you can trace through the assembly language. You can configure breakpoints in many ways. Just right-click the red breakpoint marker in the editor margin and select ‘Breakpoint properties’ from the pop-up menu. You can set a pass count to cause a break after a certain number of executions or a Boolean condition that causes a break when it evaluates to true. For example, enter this in our miles-to-kilometres project:
inputBox.Text == “100”Click the ‘Advanced’ button to to reveal even more ways of configuring breakpoints and click the Help button for an explanation of these options. You may want to watch the values of specific variables or properties. Do this by highlighting the identifier in the code and pressing [F5] to place it into the Watch List window that’s displayed when debugging. Alternatively, right-click inside the Watch List window and select Add Watch.
If you just want to check the value of a variable on the fly, press [CTRL]+[F7] to pop up the Evaluate/Modify dialog. Here you can enter a variable or an expression to examine its return value and, when appropriate, you may also change this value.


