Computer Programming: Quarter 2 - Module 1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

10

Computer
Programming
Quarter 2 – Module 1

https://www.google.com/search?
q=image+of+vb+logo&source=lnms&tbm=isch&sa=X&ved=2ahUKEwiV9ZOet7jsAhXNMN4KHQ9EAPEQ_
AUoAXoECA8QAw&biw=1046&bih=535#imgrc=g- uMvGi_eQgnM

ZENY Q. KARGANILLA
Bacnotan National High School
Science Department
Computer Programming 10
Quarter 2 – Module 1

 Variable and Constant


Target

In this learning material, you will learn the different types of data used in
a Visual Basic program code, how are variables named and how are variables
used in a program.
After going through this learning material, you are expected to:
1. describe variables;
2. name variables,
3. declare and assign values to a variable; and
4. use variables in a program.

Pretest

Direction: Choose the letter of the correct answer. Write your answer on a
separate sheet of paper.

1. What do you call the symbolic names given to values stored in memory and
declared with the Dim keyword?
A. expressions B. keywords C. operators D. variables
2. Which function in vb.net converts the displayed data to string data type?
A. Asc B. Chr C. Str D. Val
3. Which function in vb.net converts the displayed data to numeric data type?
A. Asc B. Chr C. Str D. Val
4. Keywords are also referred to as reserved words.
A. false B. true C. It might be true D. both B and C
5. A variable declared inside an event procedure is said to have local scope.
A. false B. true C. It might be true D. both B and C
6. A variable declared outside an event procedure is said to have class-level scope.
A. false B. true C. It might be true D. both B and C
7. Which of the following is NOT a valid variable data type?
A. double B. integer C. real D. string
8. What is the correct statement when declaring and assigning the value of 10 to
an integer variable called numA?
A. numA=10 B. Dim numA=
C. Dim numA=Int(10) D. Dim numA as Integer= 10
9. Which of the following is a valid name for a variable?
A. 7Eleven B. Seven.Eleven C. Seven Eleven D. Seven_Eleven
10. Which statement is TRUE about keywords in visual basic?
Keywords in visual basic are words that___________.
A. have special meaning and should not be used when naming variables.
B. are used as prefixes for control names (such as txt, btn, and lbl).
C. are used to name controls such as textbox1, textbox2, etc.
D. should be used in naming variables.
LESSON Variable and
1 Constant

Jumpstart

Activity #1: Guess Me!


Objective: Describe a variable
Direction: Analyze the figures below, then give your descriptions about a variable
based from the figures. Write your descriptions on a separate sheet of paper.
The glasses used are examples of variables, the cubes represent values,
and name tags are the names of the glasses.

Mary Marie Maria Marie Maria


Red2

Answers:
1. Variable/s is/are ___________________________________.
2. A variable holds/contains____________________________.
3. _________________________________________________.
4. _________________________________________________.
5. _________________________________________________.
6. _________________________________________________.
Discover

What are Variables?


Variables are used to store information to be referenced and manipulated in
a computer program. They also provide a way of labeling data with a
descriptive name, so our programs can be understood more clearly by the
reader and ourselves. It is helpful to think of variables as containers that hold
information. Their sole purpose is to label and store data in memory. This data
can then be used throughout your program. In some cases, data changes due
to calculations or state changes within the application. Thus, we find it
necessary to always declare variables.
When VB manipulates data, it allocates location in its memory for holding
pieces of data in the program modification during execution. It must be able to
know the memory location of where these pieces of data go. For VB to keep track of
these data, you have to assign a label to this memory location. This is called a
variable name. Variables should also contain a data type that determines the kind
of data it can store.

Naming a Variable
In naming a variable, you should be concise in describing it. It should be
meaningful to what each variable represents. The names you will use maybe
chosen from a word in the problem or to refer to a value the variable contains.
The following should be taken into account when assigning a variable name:
 The variable name should start with a letter.
 It may be followed by numbers, letters, or a combination of both types.
Special characters such as @, #, $, % and & can be used as the last
character of the variable name.
 A blank space or period is not accepted. An underscore(_) can be used to
separate words to form a variable name.
 It is advisable to use variable names in lower case for easy identification. If
you use a single two-word variable name, the first character of the second
word can be in upper case.
 The variable name should not exceed to 255 characters.
 Reserved words are not allowed to be used as variable name. Reserved
keywords such as Private, Next, Loop, Sub, End, etc.

Variable Data Types


There are quite a few VBA data types, but for the general purposes of financial
modeling not all of them are used.
Below is a list of common VBA variables (known as data types) used in macros
and their purposes:
Numerical
 Integer: Used to store number values that won’t take on decimal form.
 Single: Used to store number values that may take on decimal form. Can
also contain integers.
 Double: A longer form of the single variable. Takes up more space, but
needed for larger numbers.
Non-numerical
 Date: Stores date values.
 String: Stores text/characters. Can contain numbers, but will store them as
a text (calculations cannot be performed on numbers stored as string)
(When using strings, you have to surround the text in quotation marks. This
is not true number or binary values)
 Boolean: Used to store binary results (True/False, 1/0)

Again, there are other data types, but these are the most commonly used for
creating macros.

Declaring a Variable
We declare to simplify the writing of the program and make it run faster. VB
creates variables automatically and assigns a default value of zero(0) for numeric,
null(“”) for String and empty( ) for Variant. Undeclared variables are considered as
Variant type which use up more space, making your program run more slowly.
To declare a variable is to tell the program about the variable in advance. By
declaring a variable, the user provides information to the VBA compiler about the
variable data type and other information such as the level. The data type can
either be an integer, text, decimal, Boolean, etc., whereas the variable level can be
either procedural level(local), module-level(Private), or public scope(entire project).
A variable's scope determines where in a program a variable is available for
use. A variable's scope is defined by where the variable is initialized or created.

Dim statement
When declaring variables, you usually use a Dim statement. A declaration
statement can be placed within a procedure to create a procedure-level variable. Or
it may be placed at the top of a module, in the Declarations section, to create a
module-level variable.

The syntax for a simple declaration of a variable is as follows:


Dim variableName As variableType
In the above outline, Dim is the keyword which indicates to Visual Basic that a
variable is being declared. variableName is the name assigned to the variable. Try
to use a descriptive variable name and prefix the name with something which
indicates the variable type. For example, when declaring a String variable prefix
the name with str (e.g. strFirstName). The As keyword precedes the declaration of
the variable type (String, Date, Integer etc).
To declare an Integer value named intInterestRate for example:

Dim intInterestRate As Integer

It is also possible to declare multiple variables on the same line by separating


each variable with a comma. When the variables are of the same type, the type
declaration only needs to be made once at the end of the declaration:

Dim intInterestRate, intExchangeRate As Integer

In the case where the variables are of different types the type of variable must be
declared at the end of each group of the same type. For example:
Dim strCustomerName As String, intInterestRate, intExchangeRate As Integer

If the Dim statement appears within a procedure, the variable can be used only
in that procedure. If the statement appears in the Declarations section of the
module, the variable is available to all procedures within the module, but not to
procedures in other modules in the project.

To make this variable available to all procedures in the project, precede it with the
Public statement, as in the following example:

Public strName As String

Public statement
You can use the Public statement to declare public module-level variables.

Public strName As String


Public variables can be used in any procedures in the project. If a public variable
is declared in a standard module or a class module, it can also be used in any
projects that reference the project where the public variable is declared.

Private statement

You can use the Private statement to declare private module-level variables.

Private MyName As String


Private variables can be used only by procedures in the same module.

Note
When used at the module level, the Dim statement is equivalent to the Private
statement. You might want to use the Private statement to make your code easier
to read and interpret.

Static statement

When you use the Static statement instead of a Dim statement to declare a
variable in a procedure, the declared variable will retain its value between calls to
that procedure.

Initializing Visual Basic Variables


Visual Basic variables may be initialized either during the declaration, or after
the declaration. Unless there is a good reason to do otherwise, it is recommended
that variables be initialized during the declaration.
Initialization during the declaration is performed using the Visual Basic
assignment operator (=). To initialize a single variable when it is declared:
Dim intInterestRate As Integer = 5

When declaring multiple variables each variable may be initialized in the


declaration line:
Dim strCustomerName As String = "Fred", intInterestRate = 5 As Integer,
intExchangeRate As Integer = 10

Initialization after the declaration:

Dim intInterestRate As Integer


Dim intExchangeRate As Integer
Dim strCustomerName As String
intInterestRate= 5
intExchangeRate= 10
strCustomerName= “Fred”

What are Constants?


Constants are mechanical names or symbols that takes the place of a number
or string that represent a fixed value. The constant’s value does not change during
the program execution. A constant can be numeric, string, or Boolean type.
When you declare a constant, you should assign it a concise but descriptive
name to make your program easy to understand. In declaring a constant, refer to
the syntax below:

Syntax:
Const constantName (As type) =expression

The scope of constants has the same level of the scope of variables. If the
constant appears inside a procedure, it can be accessed only within that
procedure. To make it accessible to all procedures in the module, place the Const
keyword in the Declaration section of the module.
Below is a sample VB program using constants.

Private Sub Button1_Change()

Const pi= 3.1416


Dim x, r as Integer
r= Val(Text1.Text)
x=pi*r^2
Text2.Text = x

End Sub

Note:
Val function converts string values into numeric values

Sample Problem 1:
Design and develop a simple Check box and Text box application that when the
user clicks one of the three checkboxes, it will indicate in the text box on which
check box the user had clicked. For example, if checkbox 2 was clicked by the
user, it will display “Check box 2 is clicked!” at the text box. It will do the same
with check box 1 and check box 3. Follow the given figure below in designing and
developing the application. Apply the Dim variable declaration.

1. Open your Visual Basic 2008/2010/2017/2019.etc..


2. Create a new Windows Forms Application project by using either the File menu
or NewProject icon in the toolbar.
3. Name the new application system vbCheckbox1a.
4. Set the Form’s text property to vbCheckbox1,too.
5. Click, drag, and drop three(3) checkboxes from the Toolbox to add them to the
form, then set their respective Text property to Checkbox1, Checkbox2, and
Checkbox3. Finally, click, drag, and drop Textbox1 from the toolbox to add it
into the form.
6. Double -click the checkbox1 on the form to display the
checkBox1_CheckedChanged event method. Embed the following lines (in bold
only) to the method.
Private Sub
Dim sMessge As String
sMessage =”Checkbox1 is clicked!”
Textbox1.Text= sMessage
End Sub
7. Double -click the checkbox2 on the form to display the
checkBox2_CheckedChanged event method. Embed the following lines (in bold
only)to the method.
Private Sub
Dim sMessge As String
sMessage =”Checkbox2 is clicked!”
Textbox1.Text= sMessage
End Sub
8. Double -click the checkbox3 on the form to display the
checkBox3_CheckedChanged event method. Embed the following lines (in bold
only)to the method.
Private Sub
Dim sMessge As String
sMessage =”Checkbox3 is clicked!”
Textbox1.Text= sMessage
End Sub
9. Build and execute the application system by clicking the Start Debugging icon(
or press F5) at the Toolbar which color is a green arrow. Then, click the OK
button
Figure 1: Program codes Using Dim declaration Figure 2: Program output Using Dim declaration

Explore

Activity # 2: Valid or Not Valid?

Direction: Determine the following variable names if they are valid or not valid.
Write your answer on a separate sheet of paper.

1. my foo 6. Xyz
2. a_myName_ 7. 2myName
3. $dollar 8. _foo_
4. xy2 9. B
5. while 10. foo

Deepen
Activity 3: Radio Button Click

Direction:
Design and develop a simple Radio buttons and label application that when the
user clicks one of the three Radio buttons, it will indicate in the label on which
Radio button the user had clicked. For example, if Radio button 1 was clicked by
the user, it will display “Radio button 1 is clicked!” in the label. It will do the same
with Radio button 2 and Radio button 3. Follow the given figure below in designing
and developing the application. Apply the Dim variable declaration. Use separate
sheet of paper for your answer.

1. _________________________________________________

2.__________________________________________________

3.__________________________________________________

4.__________________________________________________

5.__________________________________________________

6.___________________________________________________

7.___________________________________________________

8.___________________________________________________

9.___________________________________________________

Gauge

Direction: Choose the letter of the correct answer. Write your answer on a
separate sheet of paper.

1. Which data type stores yes/no type of data?


A. Boolean B. Integer C. String D. Variant

2. Which function in vb.net converts the displayed data to string data type?
A. Asc B. Chr C. Str D. Val

3. Which of the following rules are TRUE in naming variables?

I. Names may contain spaces


II. Names may contain letters
III. Names may contain underscore
IV. Names may contain digits
A. I,II,III,IV B. II,III,IV C. I,III, IV D. I, II, III

4. Which of the following is NOT valid as variable name?


A. $false B. true$ C. falseTrue D. false_true

5. Which is a valid statement for declaring a variable?


A. Const Form As Integer B. Const myForm As Integer
C. Dim Form! As Integer D. Dim myForm As Integer

6. A variable declared outside an event procedure is said to have class-level scope.


A. false B. true C. It might be true D. both B and C

7. Which of the following is NOT a valid variable data type?


A. double B. integer C. real D. string

8. What is the correct statement when declaring and assigning the value of 10 to
an integer variable called a?
A. a=10 B. Dim a=
C. Dim a=Int(10) D. Dim a as Integer= 10

9. Which of the following is a valid name for a variable?


A. 7A B. A B C. A7 D. 7A&

10. Which of the following is TRUE about Dim statement?


Dim statement declares________.
A. only string variables
B. only integer variables
C. the variables
D. none of these

References:

_ https://corporatefinanceinstitute.com/resources/excel/study/vba-variable-types/
_ https://corporatefinanceinstitute.com/resources/excel/study/vba-variables-dim/
_ file:///C:/Users/ASUS/Downloads/innovative-activities-to-teach-computer-science-
concepts-inside-the-classroom-and-at-outreach-events.pdf

_ https://launchschool.com/books/ruby/read/variables
_ https://docs.microsoft.com/en-us/office/vba/language/concepts/getting-
started/declaring-variables

_
https://www.techotopia.com/index.php/Declaring_Visual_Basic_Variables_and_Constants

You might also like