0% found this document useful (0 votes)
26 views5 pages

PRACTICE 4 - Variables Constants and Data Types

This document introduces the concepts of variables, constants, and data types in Visual Basic 6.0. It explains that variables are used to store values that can change, while constants store fixed values. It details the main data types such as integers, decimals, strings, and booleans. It includes examples of how to declare variables and constants of different types and assign values to them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

PRACTICE 4 - Variables Constants and Data Types

This document introduces the concepts of variables, constants, and data types in Visual Basic 6.0. It explains that variables are used to store values that can change, while constants store fixed values. It details the main data types such as integers, decimals, strings, and booleans. It includes examples of how to declare variables and constants of different types and assign values to them.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DATABASE II

VISUAL BASIC 6.0

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..

In Visual BASIC, it is advisable to declare variables, that is, to inform Visual


BASIC that we are going to use these variables. They are declared at the beginning of
code and the reserved word Dim is placed before it, then the name that we
We want to follow the type of data that will be stored, for example if I want to store
in a variable called Number.

Dim number As Integer

L.S.C. ARMANDO CORTÉS CASTAÑÓN


DATABASE II
VISUAL BASIC 6.0

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:

Dim number As Integer

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:

Integers: within integers we have various types of data


depending on the range we want to use:

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.

Decimal numbers: to store decimal numbers we have


the following types of data:

Single: stores decimal numbers in a smaller range.


Double: stores larger decimal numbers.

Character strings: character strings are defined with the word String.
they have a range of up to 2 trillion characters.

Types of logical values: to store types of logical values such as true or


false, the word Boolean is used and its 2 possible values are True (true) and
False

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.

L.S.C. ARMANDO CORTÉS CASTAÑÓN


DATABASE II
VISUAL BASIC 6.0

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.

Unlike variables declared with the Dim keyword, constants are


they declare with the word Const.

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:

1 - In the following line, 2 types of Integer variables are declared called


num1 and num2. Then a value is assigned to each and then we add them, and
through the MsgBox function which is a Visual Basic function to display
messages, we show the result of that sum.

Place a button (CommandButton) named Command1. Double-click on the button.


to open the Visual Basic code window. Inside the Click procedure
put this code:

Dim num1 As Integer


Dim num2 As Integer

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

Dim name As String


Dim surname As String
we set values
Carlos
Pérez

we show a message with the value of the variables


MsgBox name
MsgBox last name

3 - Now an example that produces a runtime error. We create 2


variables exactly the same as in the previous example, but instead of declaring the
We define the variables name and surname as type String instead of type Integer.

Dim Name As Integer


Dim LastName As Integer
We establish numerical values
Carlos
Peres

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.

L.S.C. ARMANDO CORTÉS CASTAÑÓN


DATABASE II
VISUAL BASIC 6.0

4 - An example that stores a number in a Byte type variable (numbers from 0


First, we declare the variable and give it a name, for example if
we want to store the number 88 we do it for example like this.

Dim Age As Byte

To assign a value to that variable:

Edad = 88

To show the value of the variable Age in a TextBox control when we press
a button Command1 would be like this:

Private Sub Command1_Click ()


We declare the variable
Dim Age As Byte

We assign it a value that is not less than 0 or greater than 255


Edad = 88

We show it in the control called Text1


Age
End Sub

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.

L.S.C. ARMANDO CORTÉS CASTAÑÓN

You might also like