Programming I (Visual Basic .Net)
Method – small programs consisting of program statements that specifies actions that implement some behaviour of an object. Data is stored in the properties of the object. There is an input, a process and an output. Data is what a method processes, e.g. price of each product on sale.
VB is a high level programming language.
Instructions – program statements.
Algorithm – a solution to a problem.
Variables – named areas of a computer memory used for storage of data, e.g. name of customer, price of an item.
Variable types – e.g. balances in money can be changed but a house number cannot. A string is not meant to be processed, like the house number or a person’s name. An integer stores whole numbers. Variables of different types can be manipulated differently.
char = character – cannot be multiplied
integer = can be multiplied
long = used for storing long numbers
Identifiers – e.g. FirstNumber. second_number, thirdnumber. This is the name of a variable. The names cannot use spaces, start with a number or use End for example as it is a reserved keyword with a special function in VB. NET
|The program statements||The variables|
|Obtain the number||theNumber|
|Double the number||theResult|
|Display the result|
This program obtains a number from the keyboard, doubles the number and displays the result.
7 is assigned to the variable, theNumber. 7 is timed by 2 and 14 is stored in the variable, theResult.
Dim = dimension – set aside some memory to store the variable.
dim theNumber as Integer
CInt = convert the variable into an integer
Cstr = convert the variable into a string
A label, a picture ox is a class. The form is a class. A button can begin a process, interrupt a process and end a process. When clicked, it uses a click event. There are user and system events.
User – moving the mouse, clicking the buttons, pressing keys on the keyboard
System – time related for example like a stop clock.
change background colour by:
lblColour.BackColor = Color.White
lblColour is a label name.
A program consists of program statements and data. The data includes variables and names and identifiers.
Logical errors – errors in how the code is put together. the syntax is correct.
Compiling errors – these are syntax errors.
Runtime errors – the executable file is converted to the machine language but it errors when the .exe is run. E.g. trying to run a program that accesses the disk when it is not there. This is also a logical error as there is no detection of the disk being present to stop program crashes.
x = x + 1
This means take x and then add one to it. The variable of x increases by 1 and then stores x as one higher.
Input ->PROCESS -> Output
|2||3||–||5||A + B|
|1||2||3||6||A + B + C|
|7||1||1||5||A – (B – C)|
|1||2||3||6||A * B * C|
Moving an object:
lblMove.Top = lblMove.Top + 30 (moves it down)
lblMove.Top = lblMove.Top – 30 (moves it up)
lblMove.Left = lblMove.Left + 30 (moves it right)
lblMove.Left = lblMove.Left – 30 (moves it left)
Hiding something with a button click:
btnRed.Visable = False
Disable a button:
btnRed.Enabled = False
User tab index to set a tab order. Start with 0.
Stick a “&” before the button text to create a keyboard shortcut and it underlines the first letter. It can go at any place in the word because there might be a button text starting with the same letter.
The form really is a class. The form, the buttons, the text boxes, etc are objects. The form is a container object.
Garage collection frees up memory through automatic memory management so we do not have to tell the PC to take a button out of memory.
VB Code – colour with a scrollbar
dim myColourRed as Red
Dim redValue As Integer
redValue = hsbRed.Value
myColour.Red = Color.FromArgb (redValue, 0,0)
lblDemo.BackColor = myColourRed
lblDemo.Text = redValue
It creates a scrollbar that changes the colour and shows the colour value
Objects consist of code and variables/settings/property
State and variable mean the same thing for objects.
Behaviour can be called a service which can be called a method
Objects can’t see other objects’ data unless you allow the object to do so. It can see it through allowing a copy of the data to be seen. The object state is only allowed to be seen by an appropriate method to allow it.
One should not allow the states of objects to be seen by all the other objects. The word “private” in VB ensures that the variables aren’t allowed for other objects.