PRACTICE 4 - Variables Constants and Data Types
PRACTICE 4 - Variables Constants and Data Types
PRACTICE 4
VARIABLES, CONSTANTS AND DATA TYPES
INTRODUCTION
All programming languages use variables, constants, and different types of data, as well.
to use a methodology for application programming, but they vary in the way they do so
that are used, their syntax.
When one just starts to program, it seems very complicated to understand these.
themes, but with practice everything becomes clearer.
The good thing about understanding the fundamentals of programming is that it allows us to
then be able to program in almost any language, because the way of programming in
general does not vary too much, what changes in each of them is their syntax and
functions and specific things of the language.
VARIABLES
Variables, as their name indicates, are used to store values that have
the property of varying the content. When we talk about content, we refer to
any type of data, for example a name, a date, a color, a number, etc...
Variables are assigned a name so that they can be used. For example, I can
create a variable called date and this will store a date. To the names of the
variables are referred to as identifiers. When we create variables, we have to
try to assign them a name that relates to the type of data we want
store. For example, it wouldn't make much sense to create a variable called my there
to save or store a name or a surname, because when we look at the code
it would be more difficult to deduce what type of data I am storing. For example in this
it would be much more logical to create a variable called "names" and there
guardar "Omar", "Juan", "Natalia" etc..
The word Integer tells Visual BASIC that I am going to store an integer number.
After declaring it, we can assign a value with the operator '=', for example:
numero = 1500
But it is important to note that when declaring a variable, in this case, of type
integer, we could not store in it a string of characters such as for example
a name or any other type of data that is not an integer. Yes
this would happen our program would give a runtime error, showing us
An ugly sign telling us that the data types do not match.
DATA TYPES
Data types indicate the type of value that a variable can store.
the main types of data are:
Byte: can store integer numbers within the range from 0 to 255
Integer: can store whole numbers within the range -32,768 to
32.767
Long: can store integer numbers within the range -
2,147,483,648 to 2,147,483,648.
Character strings: character strings are defined with the word String.
they have a range of up to 2 trillion characters.
Dates and times: The word Date is used to store dates and times.
Type variant: Variables of this type can store any type of value.
but they take up more memory.
CONSTANTS
Constants, like variables, are used to store data and values for
our program, but unlike these last ones (the variables), the content that
stored no change always is constant.
Example:
Constnumber = 53
In the previous line, I created a constant, which I called number, and it will store a
number, and this value, when my program is executed, will remain unchanged.
Do the following:
num1 = 10
num2 = 20
A message will be displayed with the sum of the variables with the result
30
MsgBox num1 + num2
When running the program, you can see a message box appearing with the
result when adding the 2 variables num1 and num2
2 - Now we are going to declare and create 2 variables of type String, that is we are going to
store character strings in each of them. In the example, 2 are created
variables, one called name and the other last name. Then we assign a value, and for
last we display the variables with a message using the MsgBox function as in the
previous example. But first, it is important to clarify something, the strings in visual
BASIC is enclosed in double quotes as you can see in the example, of the
On the contrary, an error will occur.
L.S.C. ARMANDO CORTÉS CASTAÑÓN
DATABASE II
VISUAL BASIC 6.0
If you try the example, Visual BASIC will show you an error message like the
data types do not match
It shows us the error because the variables are declared as type Integer,
and you are assigning a data type that it does not accept.
Edad = 88
To show the value of the variable Age in a TextBox control when we press
a button Command1 would be like this:
If you tried changing the value 88, for example, to 300, the same would happen as in the
previous example, that is, a Runtime Error 13 "Type Mismatch"
data types.
Save the projects you completed in a folder called 'practice 4-variables and
constants.