String expressions

Before reading this note, read the note on data types. As you see there, computers work with various types of data -- string, numeric, boolean and others. We can write expressions of each data type. In this note, we will introduce string expressions. (We will cover other types of expression later).

Let's start with a simple example of a string expression: "Bob" & "Sam".

The expression consists of two string operands, "Bob" and "Sam", and the & operator. (You may use "+" instead of "&").

The value of this expression is the string "BobSam". When the expression is evaluated, the strings "Bob" and "Sam" are put together (or concatenated) to form a new string "BobSam".

Note that whenever the computer evaluates a string expression, it boils it down to a single string value. (We will see later that this is the case for all types of expressions -- they have a single value of the appropriate data type).

As we will see, expressions are used in many ways, one of the most common being in assignment statements. Run this program which uses string expressions to assign values to the Text property of labels.

As you see, it evaluates and displays the values of several string expressions. The following is a listing of the event handler for clicking on the "OK" button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
   Label1.Text = "Bob"
   Label2.Text = "Bob" & "Sam"
   Label3.Text = "Bob" & " Sam"
   Label4.Text = "Hello, " & "my " & "friend" & "!"
End Sub
Note that we are using a new form of assignment statement. When the computer executes an assignment statement with an expression on the right hand side of the equal sign, it first evaluates the expression. When it has the value, it assigns it to the left hand side.

We have used string expressions in this illustration, but the same two-step process is followed for numeric, boolean or any other type of expression. Note that both the left and right hand sides of each assignment statement are of type string. (As we have seen, the data types on both sides of the assignment statement must always be the same).

The general form of the assignment statements in this example is:

   <object name>.<property> = <expression>


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.