Expressions Assignments and Data Types

From Real Software Documentation

Jump to: navigation, search

Aim In this lesson, you will learn how to perform simple calculations; how to shift information around, and a little about Real Studio’s different data types.


Contents

A Better Greeter

Open the project from Lesson 1. We’re going to make it do a better job of greeting someone.

  1. Open Window1, and double click on the button, so that Real Studio displays the Action event handler for the button again.
    This was where we put our code in the last lesson.
  2. Change the Action event handler to read:

    MsgBox "Hello, " + UserName.Text

    Notice that there is a space after the comma, before the double quote.
  3. Save the project again, and run it.
  4. Enter your name and click the PushButton.

Much better.

What we’ve done is to provide Real Studio with a more sophisticated expression as the argument for the MsgBox command. This expression puts some text in front of the text from the UserName box.

About Expressions

You have seen expressions many times, in math class. Something like this:

3 + 4 x 5
is an expression. It tells you to do various things (adding and multiplying, in this case), and provides all the information you need to do those things (the numbers you are going to add and multiply), and after you have done those things, you will have a result. Working out the result of an expression is called evaluating the expression.

All of this also applies to expressions in computer programs, except that there are many more operations available than you use in mathematics, and they can work on a much bigger variety of things than just numbers (as we can see in our program, which has an expression that contains pieces of text).

So here is the general principle:

Anywhere in a computer program where you are supposed to supply some piece of information, you can put an expression instead.

Actually, it’s a little more complicated than that; here’s the full version:

Anywhere in a computer program where you are supposed to supply some piece of information, you can put an expression instead — as long as when that expression is evaluated, the result is of the correct type.

About Types

Every piece of information in a computer program has a type. This is because you can only do certain things with certain kinds of information. It doesn’t make sense to multiply two strings together, for example.

The important thing about a type is the operations[note 1] you can perform on something of that type.

For now, you just need to know that any description of a command or operation in the Real Studio documentation will specify what types the arguments to the command or operation should be, and what type the result should be. Real Studio will show you an error message if you try to run a program that breaks these rules.

The expression in our program uses the + symbol you are familiar with from mathematics, but in this case it doesn’t mean addition (it does mean addition if you use it with numbers). If you put strings[note 2]on either side of it, when the computer evaluates this expression, it will form a new string by putting the second string right after the first (this is formally known as concatenating the strings). Notice that in the expression in our program, we had to tell Real Studio which part of the program line was a string by enclosing it in quotes. If we don’t do that, Real Studio will try to interpret that part of the line as a command, and will produce an error when you try to run the program (we&trsquo;ll see what happens when you do that in a moment).

It is important to understand that spaces are characters just like letters and numbers. In our example, we put a space after the comma, inside the quotes, because if we hadn’t, the computer would have very obediently produced a result like:

Hello,Guy

rather than:

Hello, Guy

Experiment

Try experimenting with the expression. For example, you can make a longer sentence. How would you get the computer to produce a result like this (hint: you can use the + operator more than once in an expression)?

Hello, Guy. How are you?

An Error

Let’s see what happens if we deliberately introduce an error into our program. Change the code to read:

MsgBox "Hello, " + UserName.Text + 2

When you try to run this, Real Studio will open the Errors pane that lists the errors. Each listing contains a description of the error and the line that contains the error.

The Errors pane, showing a compile error.

An type mismatch is when you can’t perform the operation you’ve asked for with the particular types of things you’ve provided. In this case, Real Studio expects that if you’re adding a number to something, that thing should also be a number. And if you’re adding a string to something, it should be a string. If you wanted to append a 2 to a string, you must put it in quotes, like this:

MsgBox "Hello, " + UserName.Text + "2"

Again, the quotes tell Real Studio that the thing inside them is a string. By the way, did you notice that as you’ve been working in Real Studio, whenever you point at something with the mouse pointer, that line at the bottom of the window provides useful information about it?

Another Example

Let’s try a more complicated example. We’ll build a program that will add numbers together:

  1. Choose New Project from the File menu.
  2. Open Window1’s Window Editor, and drag three TextFields, a Line and a PushButton into it. Rearrange them and set the button’s caption so the window looks like this:
    Drag three TextFields, a Line, and a PushButton into Window1.

    It’s generally a good idea to give meaningful names to every important object in your program. You set the name by clicking on the object, then changing the Name property in the Properties pane.
  3. From top to bottom, name the TextFields “Source1”, “Source2” and “Result”. Name the PushButton “EvaluateButton”.
  4. Double-click on the Pushbutton and enter the following line into its Action handler.
Result.text = Str(CDbl(Source1.text) + CDbl(Source2.text))

Don’t forget to save the project at this point. We’ll be using it again in the next lesson.
To understand the expression here, you need to know a few things:

  • As well as operations and commands as we’ve seen so far, Real Studio has functions.
    When evaluated, a function supplies a value of some kind, such as a string or a number. Functions are written as a name followed by any arguments to the function between parentheses. CDbl is a function which has a single string argument, and turns that string into a number. Conversely, Str has a single argument that is a number, and turns that into a string.
  • Since you can put an expression anywhere you are supposed to supply some piece of information, one place you can put an expression is inside an expression. The computer will evaluate such expressions from the inside out[note 1].

Now we have enough information to understand what the expression above means.

Let’s look at it from the inside out:

Expression(s) Meaning
Source1.text and Source2.text Whatever has been typed into the top two boxes in Window1.
CDbl(Source1.text) and CDbl(Source2.text) The numeric value of whatever was typed into the box (these will be 0 if whatever was typed in can’t sensibly be interpreted as a number).
CDbl(Source1.text) + CDbl(Source2.text) Adding together the numbers in the two boxes.
Str(CDbl(Source1.text) + CDbl(Source2.text)) Turning the result back into a string.

Notice how we had to be sure about the type of what we had at each step of the way: the TextFields contain strings, so we had to turn these into numbers in order to add them together. The string “2” is not the same thing as the number 2, because 2 + 2 = 4, while "2" + "2" = "22"); then we had to turn the result back into a string, because the MsgBox command doesn’t accept a number.

More Errors

Even the professionals type things in wrong; here are some errors you might accidentally introduce, the error message REAL Studio displays when you try to run the program, and an explanation of what is wrong:

Code Error Message What’s wrong
MsgBox Username. Error: Parameters are not compatible with this function The argument to the command, function or operator is of the wrong type; in this case, Username is a TextField, and you can’t display an TextField — you have to display its text property: Username.Text
MsgBox "hello," Username.text Syntax Error You left out the +
MsgBox "hello," +Usernametext Error: this method or property does not exist You forgot the period between Username and text.
MsgBox "Hello, + Username" No error This one runs, but displays Hello, + Username. This shows you the importance of putting quotes in the right place.

Try changing the project so it multiplies two numbers together. The symbol for multiplication in REAL Studio is * (so we would write two times two as 2* 2). You can also do division with the / symbol. Subtraction is the symbol you expect.

If you haven’t tried it, see if the program works with numbers with a decimal point and a fraction part, or a negative number.

What happens if you convert the project to do division, and then you ask it to divide by zero?

References

Notes
  1. This may sound intimidating, but it’s something you’re probably already very familiar with. A numeric expression like 3*(4+5) consists of an expression (4+5) inside of a larger expression, and it all works exactly the same way in Real Studio, even down to using parentheses to control the order in which things are evaluated.

Previous Chapter Next Chapter

Personal tools