16. Assignment operator

Now that we’ve created our necessary variables and we’ve prepared the “Load” event and we’ve also got our pseudocode comments written, we’re ready to turn those comments into working code. The first comment we need to convert into code involves changing what the Noughts and Crosses buttons have written inside them.

Object Properties
When you set up the GUI you might recall the property you changed in order to have the first button say “X“, i.e:

Object properties are easy enough to change from the Design view of your form but what if you want them to change during the running of your program? Well this is where knowledge of variables becomes handy.

Each property of an object can be thought of as a variable, which means it can be changed just like a variable! The only difference is that to refer to it we need to use the object’s name, followed by a period, followed by the property name, i.e:

<<objectname>> . <<objectpropertyname>>

And then if you want to change that “variable”, we need to use the Assignment Operator.

Giving a variable a value
In order to give a variable a value we need to use the Assignment Operator. This can be done differently in different programming languages but in VB this is done with the equals sign:

<<left-hand-side>> = <<right-hand-side>>

When the computer encounters a line that contains the equals sign it simply evaluates or takes whatever is on the right hand side of the equals sign, and places it into the variable or location described on the left hand side. So in our case, if we want the “Text” property of the “but1” object to have nothing in it we would write a line like this:

but1.Text = blank

Seeing as though we’re treating these properties like variables, they need to have a data type just like any other variable and in this case, the text inside a button can be any string of characters, this means that we will treat it as though it is of the “String” data type.

So with that information you should be able to go ahead and complete the first step in our “Load” event.

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>