Programming for Computing 07 - 08

Chapter: Objects and UML

Module Admin   Contents, Index   Schedule  
1: Intro to VB    2: First Program and Projects    3: Variables, assignment, Strings    4: Type Conversion, InputBox, Constants    5:Built-in Functions    6: If - decisions    7: Loops - while, for    Example - find smallest    8: Scopes: local, global    9: Writing Procedures.Parameters, Functions   
10: Objects    11: Design    12: Testing    13: Graphics    14: Controls and events    15: Listboxes    16: Arrays    17: Files    18: The Command Line   

Appendices(links etc)   Additional Lectures    Tutorials (not in printed notes)     Selected solutions (not in printed notes)     Assignments    Additions and Errata   

new

The  schedule page has info on what we will do each week.

 


Section 10.0

An object is a section of program code that has been 'parcelled up' to make it easy for the programmer to incorporate it into a program.

Typically, your first year of programming will involve using objects, and later years will involve writing and using them yourself.

A button or a text box is an example of an object. We can easily drop them on a form.

 


Section 10.1

Properties of an object

Examples of a button's properties are Text and enabled. You can set them at design time, but can also access them as the program runs, as in:

Button1.Text = "Go"        'alter Button1's Text to Go
Button2.Enabled = false       'set Button2's enabled to false
s = Button3.Text           'get Button3's Text, store it in s
Note that just saying Text = "Go" or Button3 = "Go" is not enough - we often have several buttons, and need to be specific about which button, and which property.

State of an object is its property values

The set of values of (say) button2's properties is called its 'state'. When we set a property, by something like:

Button2.Text = "Stop"
then the Text stays set to Stop until we decide to change it - thus, an object has a memory.

 


Section 10.2

'Get' and 'Set' methods

Often we use the jargon words of get and set. We can SET a property, as in:

Button2.Text = "Warn"
or we can recall what it was previously set to. I.e. we can GET a property, as in:

x = Button2.Text
NB - in Java, we would write the above like :

Button2.setText("Warn")                     set
x = Button3.getText()                       get

 


Section 10.3

Classes and Objects

When the hotshot programmers at Microsoft write the code for a button (which you will eventually use by dragging onto a form) they do not know how many buttons you will need, or what you want to name them.

What they actually write is termed a 'class'. (Thus, there is a Button class, a TextBox class etc)

A class is a kind of 'factory' which can give you as many objects as you need. You could imagine the button icon in the VB toolbox as the Button class, and the buttons which you drop on the form as being objects of the class (i.e. the type) Button.

It is rather like shopping in Argos. You can point at a photo of an mp3 player in the catalog, but you know that it is not a real physical mp3 player. The photo is a type of thing, or a class. You can buy as many of these as you like. The actual real things you buy are objects, not classes.

The jargon is:

An object is an instance of a class

We can create as many instances (objects) as we need from a class. Normally you do this via drag and drop, but if you look at the cocealed code of your program, you will see this creation in code, using the New keyword, similar to:

Button3 = New Button()
 


Section 10.4

UML - Unified Modelling Language

This is a design notation for classes - you will see much more of it in the future. Here is UML being used to show the properties of a class:
[uml2.jpg]
uml properties

Here is an example of UML being used to show the classes that are used in a program.

Imagine that our program is a calculator, with several buttons ( 0, 1, 2, 3... + - / etc) and a textbox for the result. The UML diagram is:

[uml1.jpg]
uml class diagram

The assumption is made that the calculator program is itself a class - which can be re-used. In Java (and vb.net) all programs are classes.

All is says is that our program uses instances (objects) from the Button and TextBox classes. It says nothing about the logic of the program.

We might also combine the above 2 diagrams, by adding the property names available for each class, as in:

[uml3.jpg]
full class diagram

Problems

1. Draw a UML diagram for a GUI program you have written.

2. Here is an e.g. of the CInt function being used.


n = Cint(TextBox1.Text)
Explain why CInt is not an object. (Think about states)

Module Admin   Contents, Index