Data Types and Modules |
|
Variables |
|
Any programming language requires to store values temporarily for different purposes like calculations Adding, multiplying etc.Visual Basic gives the need for variables. You can think of variable, a place in memory of computer for an unknown value. |
For example when you call someone, then you call him /her by name, like that every variable has name. |
Variable name is the word which you use to refer to the values that the variable contains. |
|
VARIABLES:- Naming convention |
|
There are different rules for assigning names to variables ,these are as follows: |
|
A variable name must begin with a letter. |
|
For example: |
6date - Invalid variable name |
date6 - valid name |
|
Two variables must not contain the same name. |
|
It can not contain a dot or space. |
|
For example: |
Num2 - Valid variable name |
Num.2 - Not Valid |
Num 2 - Not Valid |
|
A variable name must not exceed 255 chracters. |
|
A variable name must not contain any special characters like %, $ , * , & ,! ,@ etc. |
|
For example: |
@num, num$r are not valid variable names. |
|
Variable:-Declaring |
|
The term declaring a variable means that the memory is allocated .This memory will be referred by a name decided by the programmer.The specific data type decides the amount of memory allocated. |
You can declare a variable with a Dim statement by giving a name for the variableaccording to rules of naming conventions. |
|
Syntax: Dim variablename as Datatype |
|
Example: Dim a as integer |
In above example 'a' is variable name and integer is the data type. |
|
Rules for declaring variables: |
(i) Declaring a variable using the word Public makes it available throughout the program. |
(ii) Declaring a variable using the word Static keeps it's value preserve until a procedure ends. |
(iii) Declaring a variable in the declaration section of the form makes it available to all procedures in that module. |
|
For example: |
Let us consider the variable 'var1 '. An integer variable 'var1' is declared. At the same time memory is allocated for the variable. |
The variable is given value 20 during the running state of the program. This value is stored in the given memory location. |
|
This process is shown in figure: |
|
 |
|
|
|
|
Data Types and Modules |
|
Constants |
|
Constants as the name suggests , never change during the execution of program.They remain same throughout the program execution. |
When the user wants a value that never changes, a constant can be declared and created. |
|
The syntax for declaring a constant is: |
|
(Public|Private) Const constantname (As type) = expression |
|
The const statement can be declared as: |
|
Public const pie as single=3.14 |
|
|
|
|
|
Data Types and Modules |
|
Modules |
|
A module is a set of functions and a set of data members.Code in Visual Basic is stored in the form of Modules. Collection of general procedures, variable declarations, and constant definitions used by application is known as module. |
|
Mainly Visual Basic supports 3 types of modules: |
|
(1) Form module |
(2) Standard Module |
(3) Class Module |
|
Form module |
|
Form modules provide the user interface to the application. They contain controls and properties of the forms. They have the extension .FRM. |
Their declaration is private by default, therefore each form has a single form module associated with it. |
|
Standard Module |
|
As we go further in developing the application than there may be common code for execution of several form. |
For avoiding the duplication of code, a separate module containing the code is implemented. |
This is called as standard module. A standard module (.BAS) contains only code. It contains extension .BAS. |
|
Class Module |
|
A class module (.CLS) is used to create objects (forms etc.). It can be called from procedures within your application. |
Class modules (.CLS filename Extension) are writing code in class modules can create the building blocks of object oriented programming in Visual Basic.New objects. |
|
Each module contains different elements, these are: |
|
Declaration |
It includes constant and procedures. |
|
Procedures |
Sub function or property procedures that contain pieces of code that can be executed as a unit. It avoids code repetition. |
|
|
|
|
|
|
|
No comments:
Post a Comment