Senin, 29 Maret 2010

Visual Basic 6 (VB6) Data Types, Modules and Operators

Visual Basic uses building blocks such as Variables, Data Types, Procedures, Functions and Control Structures in its programming environment. This section concentrates on the programming fundamentals of Visual Basic with the blocks specified.

Modules

Code in Visual Basic is stored in the form of modules. The three kind of modules are Form Modules, Standard Modules and Class Modules. A simple application may contain a single Form, and the code resides in that Form module itself. As the application grows, additional Forms are added and there may be a common code to be executed in several Forms. To avoid the duplication of code, a separate module containing a procedure is created that implements the common code. This is a standard Module.

Class module (.CLS filename extension) are the foundation of the object oriented programming in Visual Basic. New objects can be created by writing code in class modules. Each module can contain:

Declarations : May include constant, type, variable and DLL procedure declarations.
Procedures : A sub function, or property procedure that contain pieces of code that can be executed as a unit.

These are the rules to follow when naming elements in VB - variables, constants, controls, procedures, and so on:
  • A name must begin with a letter.
  • May be as much as 255 characters long (but don't forget that somebody has to type the stuff!).
  • Must not contain a space or an embedded period or type-declaration characters used to specify a data type; these are ! # % $ & @
  • Must not be a reserved word (that is part of the code, like Option, for example)
  • The dash, although legal, should be avoided because it may be confused with the minus sign. Instead of First-name use First_name or FirstName.

Data types in Visual Basic 6

By default Visual Basic variables are of variant data types. The variant data type can store numeric, date/time or string data. When a variable is declared, a data type is supplied for it that determines the kind of data they can store. The fundamental data types in Visual Basic including variant are integer, long, single, double, string, currency, byte and boolean. Visual Basic supports a vast array of data types. Each data type has limits to the kind of information and the minimum and maximum values it can hold. In addition, some types can interchange with some other types. A list of Visual Basic's simple data types are given below.
Data type                     Storage size                              Range

Byte                                  1 byte                           0 to 255
Boolean                             2 bytes                         True or False
Integer                              2 bytes                        -32,768 to 32,767
Long (long integer)             4 bytes                        -2,147,483,648 to 2,147,483,647
Single                                4 bytes                         (-3.4x10-38) - (+ 3.4x1038)
Double                              8 bytes                         -1.79769313486232E308 to -4.94065645841247E-324
Currency (scaled integer)    8 bytes                         -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Date                                  8 bytes                         January 1, 100 to December 31, 9999
Object                               4 bytes                         Any Object reference
String (variable-length)      10 bytes + string length    0 to approximately 2 billion
String (fixed-length)            Length of string             1 to approximately 65,400
Variant (with numbers)      16 bytes                         Any numeric value up to the range of a Double
Variant (with characters)    22 bytes + string length    Same range as for variable-length String
User-defined (using Type)   Number                          The range of each element is the same as the range of its
                                                                             data  type.

In all probability, in 90% of your applications you will use at most six types: String, Integer, Long, Single, Boolean and Date. The Variant type is often used automatically when type is not important. A Variant-type field can contain text or numbers, depending on the data that is actually entered. It is flexible but it is not very efficient in terms of storage.

Operators in Visual Basic

Arithmetical Operators

Operators                          Description                         Example                            Result

    +                                      Add                                 5+5                                   10
    -                                   Substract                            10-5                                     5
    /                                      Divide                              25/5                                     5
    \                                Integer Division                       20\3                                     6
    *                                    Multiply                             5*4                                    20
    ^                             Exponent (power of)                  3^3                                    27
 Mod                          Remainder of division              20 Mod 6                                 2
&                               String concatenation        "George"& " "&"Bush"            "George Bush"


Relational Operators

Operators                      Description                          Example                              Result
     >                             Greater than                          10 > 8                                  True
     <                               Less than                            10 < 8                                  False
    >=                    Greater than or equal to                 20>=10                                 True
    <=                      Less than or equal to                   10<=20                                 True
    <>                           Not Equal to                            5<>4                                  True
     =                                Equal to                               5=7                                    False


Logical Operators

Operators                                                            Description
   OR                                          Operation will be true if either of the operands is true
  AND                                        Operation will be true only if both the operands are true
»»  read more

Getting Started with Visual Basic 6.0 II

Project Explorer

Docked on the right side of the screen, just under the tollbar, is the Project Explorer window. The Project Explorer as shown in in figure servres as a quick reference to the various elements of a project namely form, classes and modules. All of the object that make up the application are packed in a project. A simple project will typically contain one form, which is a window that is designed as part of a program's interface. It is possible to develop any number of forms for use in a program, although a program may consist of a single form. In addition to forms, the Project Explorer window also lists code modules and classes.
                                                                    Figure 3 Project Explorer


Properties Window

The Properties Window is docked under the Project Explorer window. The Properties Window exposes the various characteristics of selected objects. Each and every form in an application is considered an object. Now, each object in Visual Basic has characteristics such as color and size. Other characteristics affect not just the appearance of the object but the way it behaves too. All these characteristics of an object are called its properties. Thus, a form has properties and any controls placed on it will have propeties too. All of these properties are displayed in the Properties Window.


Object Browser 

The Object Browser allows us to browse through the various properties, events and methods that are made available to us. It is accessed by selecting Object Browser from the View menu or pressing the key F2. The left column of the Object Browser lists the objects and classes that are available in the projects that are opened and the controls that have been referenced in them. It is possible for us to scroll through the list and select the object or class that we wish to inspect. After an object is picked up from the Classes list, we can see its members (properties, methods and events) in the right column.

A property is represented by a small icon that has a hand holding a piece of paper. Methods are denoted by little green blocks, while events are denoted by yellow lightning bolt icon.

Object naming conversions of controls (prefix)

Form -frm
Label - lbl
TextBox - txt
CommandButton - cmd
CheckBox  - chk
OptionButton  - opt
ComboBox - cbo
ListBox - lst
Frame - fme
PictureBox - pic
Image - img
Shape - shp
Line - lin
HScrollBar - hsb
VScrollBar - vsb
»»  read more
 

flagcounter

free counters