Computer Programming: Quarter 2 - Module 1
Computer Programming: Quarter 2 - Module 1
Computer Programming: Quarter 2 - Module 1
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
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
Answers:
1. Variable/s is/are ___________________________________.
2. A variable holds/contains____________________________.
3. _________________________________________________.
4. _________________________________________________.
5. _________________________________________________.
6. _________________________________________________.
Discover
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.
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.
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 statement
You can use the Public statement to declare public module-level variables.
Private statement
You can use the Private statement to declare private module-level variables.
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.
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.
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.
Explore
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.
2. Which function in vb.net converts the displayed data to string data type?
A. Asc B. Chr C. Str D. Val
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
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