55
When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs the code there. In this case the code says… btnEamonnDemo.Text = “I was pressed!” So the Text property of the button is changed, and we see the result.

When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs the code there.

In this case the code says… btnEamonnDemo.Text = “I was pressed!”

So the Text property of the button is changed, and we see the result.

Page 2: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

This program is slightly different to the previous one.

Note that we now want the text that we will assign the button to read “Fink Nottle”

Page 3: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Once again, this program is only a trivial variation of the last program

We now want the text that we will assign the button (after the click event) to read “777”

Page 4: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

It is to tedious to show the full screen for all teaching examples (and the text is too hard to read on the screen). So from now on, I will just show the relevant text, as in the above left.

btnEamonnDemo.Text = "777"

Page 5: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = “999”

Page 6: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = “1 + 2”

Not this!Everything inside the double quotes is a literal string, and appears exactly as is when displayed

Page 7: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = "Bertie" & " Wooster"

We can join two literal strings together with the & symbol. (“&” is pronounced “ampersand”)

In computer science, “joining” two strings is called concatenation

Page 8: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = "Bertie" & " Wooster"

Note the space before “Wooster”. Recall these are literal strings, we get exactly what we specify.

Page 9: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = "Bertie" & " Wilberforce" & " Wooster"

We can use multiple concatenations to build long complex sentences

Page 10: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = "Bertie" & " Wilberforce" & " Wooster"

In future slides, I won’t bother showing the initial state of the program (the “Press Me!”), whenever its existence it obvious from context

Page 11: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = "Bingo Little"

Page 12: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = “1 + 2”

Not this!

Let us return to a previous example. We decided that the text above appears as a literal string.

Suppose however we wanted the other case to happen. We want the math to be performed, and the result to be displayed…

Page 13: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = (1 + 2).ToString

The ToString method converts whatever is on the left side of the period and converts it to a string.

We should never do

… = ("1" + "2").ToString

Anymore than we could do

… = (“Fish" + “John").ToString Addition is not meaningful for literal strings

We should never do

… = ("1" + "2").ToString

Anymore than we could do

… = (“Fish" + “John").ToString Addition is not meaningful for literal strings

Page 14: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = (2 + 2 + 2).ToString

The ToString method works exactly like you might expect it to. It generalizes to longer and more complicated mathematical expressions (as we will see later)

Page 15: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = "Today I am " & (5).ToString & " years old!"

We can use the ToString method with the concatenation operator (&) to build up long expressions

…= "Today I am " & (5).ToString & " years old!"

Page 16: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

“5”5

These two things are very different for our purposes

“5” is a literal string, that happens to contain the character we use to denote five.As a literal string the following code is legal…

btnEamonnDemo.Text = “5” & “5”

5 is a literal number (or just a number), that represents the mathematical intuition of five.As a number the following code is legal…

btnEamonnDemo.Text = (5 + 5).ToString

Page 17: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Precedence

btnEamonnDemo.Text = (2 + 6 / 2 ).ToString or

The following code seems ambiguous…

Is it…

2 + 6 / 2

8 / 2

4

2 + 6 / 2

2 + 3

5

or

In fact, there is no ambiguity, because of the rules of precedence…

Page 18: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Operators Operations

() Parenthesis

^ Exponentiation

* / Multiplication and Division

+ - Addition and Subtraction

do..

first

later

last

2 + 6 / 2

2 + 3

5

So in our example, first do the expression in Parenthesis

There none in this example, next we do Exponentiation

There none in this example, next we do Multiplication and Division

So we divide 6 by 2 to get 3, next we doAddition and Subtraction

So we add 2 and 3 to get 5, we are done.

Page 19: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Operators Operations

() Parenthesis

^ Exponentiation

* / Multiplication and Division

+ - Addition and Subtraction

do..

first

later

last

(2 + 6) / 2

8 / 2

4

In this example, first do the expression in Parenthesis

So we add 2 and 6 to get 8, next we do Exponentiation

There none in this example, next do Multiplication and Division

So we divide 8 by 2 to get 4. next doAddition and Subtraction

But we are down to a single thing, 4. So we are done

Page 20: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Operators

()

^

* /

+ -

do..

first

later

last

In this example, first do the expression in Parenthesis

Inside the parenthesis we find new expression, so we start work on just this expression, using the rules of precedence…

When we are finished with the contents of the parenthesis, we are left with a single number (in this case 11), we replace the entire parenthesis with 11, and work on the new expression…

(2 + 3 * 3 ) ^ 2

2 + 9

11 ^ 2

121

(2 + 3 * 3 ) ^ 2How about

Page 21: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

btnEamonnDemo.Text = (2 + 6 / 2).ToString

btnEamonnDemo.Text = (2 + (6 / 2)).ToString

Sometimes, we use unnecessary parenthesis in our code, just to make our meaning explicit…

The two lines of code above are logically identical, but the second one “jumps out” in saying, I want 6/2 to happen first

Page 22: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Variables (chapter 3 in text)

In most computer problems, we need to remember (store) information.For example, a program that…

• …calculates your GPA, needs to store all your grades• …does your taxes, needs to store…• …converts temperature, needs to store…

We can store this information in variables

Page 23: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Computer Memory

Imagine this is a computer memory. It is full of “containers” that can store information. I want to store my shoe size (12) and my age (21)

Lets do it…

Page 24: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Computer Memory

There are a few problems with my naive example. • How can we tell which is which?• How do we know what type of data it is?

12

21

We need to give each container a name, and a type

Page 25: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Computer Memory

12

21

We need to give each container a name, and a type

Now I can access the information, and update it if necessary.

Shoe

Size

Age

integer integer

So every container has a• name • type • value

So every container has a• name • type • value

Page 26: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

In the previous slide we talked about the name and type of information

What types are there?

There are quite a few different types, let us begin by looking at numbers. (there are many types of numbers!)

Integers (whole numbers) are a common type of information we use in everyday life.

The following are always integers: The number of children a woman has, your net worth in pennies, the number of spokes on a bicycle wheel, european shoe sizes, the number of bags you can check onto an airplane….

Page 27: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intEamonnsAge As Integer

intEamonnsAge = 25

btnEamonnDemo.Text = intEamonnsAge.ToString

• In line one I am declaring a variable, by giving its name, and its type

• In line two I am assigning the variable the value 25.

• In line three I am converting the variable to a string, then assigning it to the text property of btnEamonnDemo (and therefore displaying it)

Page 28: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

I named my variable intEamonnsAge , what are the general rules for naming variables?

• Variables that are of type integer should start with “int” (other types have other 3 letter abbreviations, we will see them soon)

• After the prefix “int” we can have any combination of letters, underscores, or digits.• The name can be as small as one letter or as large as 255 letters, underscores, and digits combined.• The name cannot be a keyword

Legal intMyAgeintMy_AgeintTakeHomePayintTake_Home_PayintMaxSpeed

Illegal DimintGuess_My_Age?intTake-Home-PayintMax&MinintLock Combination

Legal, but badMyAgeintmyageintTHPintX

Page 29: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intEamonnsAge As Integer

intEamonnsAge = 25

intEamonnsAge = intEamonnsAge + 1

btnEamonnDemo.Text = intEamonnsAge.ToString

Variables can appear on the left side of assignment.

Page 30: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intEamonnsAge As Integer

Dim intMartinsAge As Integer

intEamonnsAge = 25

intMartinsAge = intEamonnsAge + 7

btnEamonnDemo.Text = intMartinsAge.ToString

I have a brother, Martin, who is 7 years older than me…

It is important to note that after

intEamonnsAge is assigned to 25, it is NOT changed in the next line of code.Variables are only changed if they are on the left side of an assignment

Page 31: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intEamonnsAge, intMartinsAge As Integer

Dim intEamonnsAge As Integer

Dim intMartinsAge As Integer

Either of the above are legal (and logically equivalent) ways to define two or more variables of the same type.

Page 32: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Assume that you know a couple, John and Mary

You know that Mary is 22 years old

You know that John is ten years her senior

Write the code that will display the average age of the couple

Dim intEamonnsAge As Integer

intEamonnsAge = 25

btnEamonnDemo.Text = intEamonnsAge.ToString

Here is some code that have seen before as a hint

Page 33: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intJohnsAge As Integer

Dim intMarysAge As Integer

intMarysAge = 22

intJohnsAge = intMarysAge + 10

btnEamonnDemo.Text = ((intMarysAge + intJohnsAge) / 2).ToString

Page 34: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intAge As Integer

intAge = 25

btnEamonnDemo.Text = "I was " & intAge.ToString & ", I will be " & (intAge + 1).ToString

…= "I was " & intAge.ToString & ", I will be " & (intAge + 1).ToString

Note that the variable name intAge should probably be something like

intCustomersAge or

intAgeAtFirstMarrige

I am using short variable names to save space

Page 35: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intAge As Integer

btnEamonnDemo.Text = "I was " & intAge.ToString & ", I will be " & (intAge + 1).ToString

Suppose we did the above, what would happen?

VB assigns all the number variables to zero by default, so the output would be “I was 0, I will be 1”.

Although VB does this for us, it is considered very bad practice to assume it. So never use a variable before you assign it.

Using a variable means (informally) displaying it, or using it on the right side of an assignment

?

?

Page 36: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim intAge As Integer = 25

btnEamonnDemo.Text = "I was " & intAge.ToString & ", I will be " & (intAge + 1).ToString

VB allows use to assign a variable at the same time as we declare it (as above).

This is called initializing a variable

Page 37: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

There are 3 integer types in VB

Short data type is for values between –32,768 and 32,767.

Integer data type can store a value from –2,147,483,648 to 2,147,483,647.

Long can store a value from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Data Type Prefix

Integer int

Long lng

Short sht

Dim shtAge As Short

Dim intCountryPopulation As Integer

Dim lngSecondsSinceBigBang As Long

Page 38: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Computer Memory

32,767

2,147,483,647

9,223,372,036,854,775,807

Short

Integ

er

Long

Dim shtAge As Short

shtAge = 32767

shtAge = shtAge + 1

overflow error

Page 39: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim shtA As Short

Dim intB As Integer

Dim lngC As Long

shtA = 10

intB = shtA + 5

lngC = intB + 115

32,767

2,147,483,647

9,223,372,036,854,775,807

Short

Integ

er

Long

It is always safe and legal to assign one the whole number types to a larger whole number type.

Page 40: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim shtA As Short

Dim intB As Integer

Dim lngC As Long

lngC = 10

shtA = lngC + 5

32,767

2,147,483,647

9,223,372,036,854,775,807

Short

Integ

er

Long

It may be safe to assign one the whole number types to a smaller whole number type.

By default VB lets use do this, making it our responsibly to make sure we don’t overflow.

It is possible to tell VB not to allow this, by typing Option Strict On as first line of code in our program

Page 41: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs
Page 42: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub bntBlueDemo_Click(…

bntBlueDemo.Text = "You pressed blue"

End Sub

Private Sub bntRedDemo_Click(….

bntRedDemo.Text = "You clicked red"

End Sub

End Class

Windows From Designer generated code

Page 43: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub bntBlueDemo_Click(…

Dim intA As Integer

intA = 25

bntBlueDemo.Text = intA.ToString

End Sub

Private Sub bntRedDemo_Click(….

bntRedDemo.Text = "You clicked red"

End Sub

End Class

Windows From Designer generated codeThis is legal

Page 44: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub bntBlueDemo_Click(…

Dim intA As Integer

intA = 25

bntBlueDemo.Text = intA.ToString

End Sub

Private Sub bntRedDemo_Click(….

bntRedDemo.Text = intA.ToString

End Sub

End Class

Windows From Designer generated codeThis is not legal!

intA was declared inside of bntBlueDemo_Click, so it can only be used inside that block of code.

More formally we say, the scope of intA is bntBlueDemo_Click

Or we might say that intA is local to bntBlueDemo_Click

Page 45: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub bntBlueDemo_Click(…

Dim intA As Integer

intA = 25

bntBlueDemo.Text = intA.ToString

End Sub

Private Sub bntRedDemo_Click(….

Dim intA As Integer

intA = 105

bntRedDemo.Text = intA.ToString

End Sub

End Class

Windows From Designer generated code

We could declare a variable called intA inside both Subs, and VB would keep them separate and never confuse them.

This might be a bad idea however, we might get confused when modifying the program

It is critical to note that the two variables here completely independent

Page 46: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Dim intA As Integer

Private Sub bntBlueDemo_Click(…

intA = 0

bntRedDemo.Text = (intA).ToString

End Sub

Private Sub bntRedDemo_Click(….

intA = intA + 1

bntRedDemo.Text = intA.ToString

End Sub

End Class

Windows From Designer generated code

There may be occasions when we want two or more Subs, to be able to manipulate the same variable.

We can do this, by having a variable with global scope

Now that intA has global scope, it can be modified anywhere in the program

Page 47: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

There are 3 real number types in VB

Data Type Prefix

Single sng

Double dbl

Decimal dec

Dim sngShoeSize As Single

Dim dblAverageInsectMass As Double

Dim decProtonMass As Decimal

Single (single-precision floating-point) 4 bytes

-3.4* 1038 to -1.4 * 10-45 for negative values; 1.4 * 10-45 to 3.4 * 1038 for positive values

Double (double-precision floating-point) 8 bytes -1.7 * 10308 to -4.9 * 10-324 for negative values; 4.9 * 10-324 to 1.7 * 10308 for positive values

Decimal 14 bytes +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;

+/-7.9228162514264337593543950335 with 28 places to the right of the decimal;

smallest non-zero number is +/-0.0000000000000000000000000001

Page 48: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Single

Double

Decim

al

It is always safe and legal to assign one the real number types to a larger real number type.

This is sometimes called promotion

Dim sngA As Single

Dim dblB As Double

Dim decC As Decimal

sngA = 101.505

dblB = sngA

bntRedDemo.Text = dblB .ToString

Page 49: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

What happens if we assign one the real number types to a smaller real number type?

This is sometimes called demotion

We may lose precision!

Dim sngA As Single

Dim dblB As Double

Dim decC As Decimal

decB = 101.50005

sngA = DecB

bntRedDemo.Text = sngA.ToString

Note the value is “101,5001”, not “101,50005”

Page 50: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim sngShoeSize As Single

Dim intA As Integer

sngShoeSize = 12.5intA = sngShoeSize

bntRedDemo.Text = intA.ToString

Note “12”, not “12.5”

What happens if we try to assign one of the whole number types to one of the real number types?

By default VB lets use do this (and the demotion shown in the last slide), making it our responsibly to make sure what we are doing makes sense.

Note VB uses “rounding” here

Page 51: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Dim sngShoeSize As Single

Dim intA As Integer

sngShoeSize = 12.5

intA = sngShoeSize

bntRedDemo.Text = intA.ToStringIt is possible to tell VB not to allow this, by typing Option Strict On as first line of code in our program

Note that Option Strict On prevents any type conversions, except promotions.

Page 52: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub bntBlueDemo_Click(…

‘This section of code written by Bingo Little, [email protected] on Feb 2 2004

‘This code allow the use to check that the default setting was correctly chosen

Dim intShoeSize As Integer ‘Note we are assuming European shoes sizes

intShoeSize = 25 ‘We using a default size of 25, per rule 123 of handbook

bntBlueDemo.Text = intA.ToString ‘Display the value so that the user can check it

End Sub

End Class

Windows From Designer generated code

Comments in VBTwo uses of comments

• annotation• temporary removing of code

Page 53: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Child_Payment_Click(…

Dim sngtotalChild As Single

sngtotalChild = 25 * 1.07

…..

End Sub

Private Sub Senior_Payment_Click(….

Dim sngtotalSenior As Single

sngtotalSenior = 12 * 1.07

….

End Sub

Private Sub Food_Payment_Click(….

Dim sngtotalFood As Single

sngtotalSenior = 231 * 1.07

….

End Sub

End Class

ConstantsSuppose we have a long program that has to calculate (7%) sales tax a lot.

Then the value 1.07 will be scatter all throughout our program…

Suppose we have one typo? Suppose we need to change the tax rate? Is this code readable?

(next slide…)

Page 54: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Child_Payment_Click(…

Dim sngtotalChild As Single

sngtotalChild = 25 * sngTaxRate

…..

End Sub

Private Sub Senior_Payment_Click(….

Dim sngtotalSenior As Single

sngtotalSenior = 12 * sngTaxRate

….

End Sub

Private Sub Food_Payment_Click(….

Dim sngtotalFood As Single

sngtotalSenior = 231 * sngTaxRate

….

End Sub

End Class

ConstantsA potential solution is to use a global variable…

The code is more readable

If we need to change the tax rate, we need only do it in one place.

But, there is a danger that we might change the variable by mistake!

Dim sngTaxRate As Single

Page 55: When the button is clicked, the program control goes to the section marked… Private Sub btnEamonnDemo_Click (marked above with a black arrow) and runs

Public Class Form1

Inherits System.Windows.Forms.Form

Private Sub Child_Payment_Click(…

Dim sngtotalChild As Single

sngtotalChild = 25 * sngTaxRate

…..

End Sub

Private Sub Senior_Payment_Click(….

Dim sngtotalSenior As Single

sngtotalSenior = 12 * sngTaxRate

….

End Sub

Private Sub Food_Payment_Click(….

Dim sngtotalFood As Single

sngtotalSenior = 231 * sngTaxRate

….

End Sub

End Class

ConstantsA best solution is to use a Constant value

(Constant can be global or local, exactly like variables variable)

Const sngTaxRate = 1.07

Const sngTaxRate = 1.07

sngTaxRate = 1.05

Const sngTaxRate = 1.07

sngTaxRate = 1.05Illegal!