VB - Data Types

Page 1

INTRODUCTION  User Interface

The user interface of a program is the visible part of the program that presents information to the user and obtains input from the user.

 The user interface of a Windows program usually includes: - Title or caption bar - Menus - Label boxes - Text boxes - Buttons

- Windows - Scroll bars


INTRODUCTION  User interface for calculator.


DATA TYPES  STATEMENTS A statement is a line of code in Visual Basic program. It is combination of keywords, properties, functions, operators and symbols that collectively creates a valid instruction for the Visual Basic compiler to work with. For example, Label1.Caption=Date is a statement that assigns the current date to the Caption property of a label box, causing the date to be displayed in the box.


DATA TYPES  COMMENTS Comment statements are statements that help explain your code in a program. A comment statement will ignored by the computer when you run your program. Comments are useful to people reading your program code, and even to yourself, several months after you have written a program. Including comments in a program is considered good programming practice.


DATA TYPES  There are two ways to indicate a comment. The

most common method is to use a single quotation mark.

 For instance, you could include a comment such as 1. ‘ this program displays the current date @ 2. Rem this program displays the current date


DATA TYPES  DATA TYPES Computing in Visual Basic may require processing several type of data. For example, you may work with amounts in dollars, whole numbers (to indicate the number of people required), or decimals. Like all programming languages, Visual Basic requires you to tell what type of data a data value is. Generally, data values are either numeric or nonnumeric.


DATA TYPES  Numeric Data ďƒ˜ Generally, numeric data falls into two categories: - Integers. Whole numbers without decimal such as 123,0,-123. For instance, integers represent ages, number of years. - Decimals. Numbers with decimal points such as 8.123 and -21.2121. for instance, decimal represent temperatures, distance value.


DATA TYPES ďƒ˜ Integers and decimals are stored differently inside Visual Basic, and they are treated differently. For example, -7 is not the same as 7.00 to Visual Basic. ďƒ˜ Visual Basic supports several numeric data types, summarized in the following table:


DATA TYPES Data type

Size

Range

Byte

1 byte

0 through 255

Integer

2 bytes

-32,768 through 32,767

Long

4 bytes

-2,147,483,648 through 2,147,483,647

Single

4 bytes

-3.402823E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.402823E+38 for positive values.

Double

8 bytes

-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values

Currency

8 bytes

-922337203685477.5808 through 922337203685477.5808


DATA TYPES  Strings A string is a non-numeric data consisting of a series of none or more characters. A string may contain numeric digits, but is never used for calculations. Examples of string include names, addresses, telephone numbers, postal codes and other data that you do not need to perform calculations. You can use quotes to indicate a string value: “mohd azwan” “012-3456789”


DATA TYPES  & Operator You can combine strings using the operator &. The process of combining strings is called concatenation. Previous version of Visual Basic and older BASIC languages use the ‘+’ to combine strings. Although you can still use + to combine strings, it is recommended that you use & to avoid confusion with the mathematical addition operator. For example: “mohd azwan” = “mohd “ & “azwan”


DATA TYPES  Other Data Types The following table summarizes the non numeric data supported by Visual Basic: Data type

Size

Range

String

1 byte per character

0 through 65,535 character

Boolean

2 bytes

True or False

Date

8 bytes

January 1, 100 through December 31, 9999

Variant (text)

Length +22 bytes

Same as variable – length string

Variant (numeric)

16 bytes

Any value as large as Double


DATA TYPES  VARIABLES  A variable is a temporary storage location for data in a program. It is an item whose value can change during the running of a program, hence its name.  For instance, you store 63 inside a variable. The value 63 never changes. The variable will hold this value until you store another value there; then the variable will hold the new value.


DATA TYPES  Variable Names It is necessary to assign a name to a variable. Variable names must adhere to the following rules:  Begin with a letter, not a number or other character.  Must contain only letters or numbers in the name.  Can contain the underscore character (_). No spaces or other punctuation mark allowed.  Can be from 1 to 255 characters in length.  Cannot be a Visual Basic keyword.


DATA TYPES  DECLARING VARIABLES  Dim Statement Before you can use a variable, you need to declare it. This is usually done at the beginning of a procedure. The Dim (for Dimension) statement declares variables by assigning them a name and data type. To declare a variable, type the variable name after a Dim statement. This action reserves room in memory for the variable when the program runs, and lets Visual Basic know what type of data it should expect to see later.


DATA TYPES  The format of the Dim statement is Dim VarName As DataType  For instance, Dim Age As Integer  You can also combine variable declarations in one Dim statement, separated by a comma, but you must use the As DataType clause for each variable. Dim Total As Integer, pi As Double


DATA TYPES  Variant Data Type

 When you declare a variable, Visual Basic normally reserves space for a variable type called a variant, which is a variable that can hold data of any size or format. Variant is flexible, general purpose type of variable, which may be the only type you will need in your program.  However, if variable is to contain small integer values, you can save space in memory when you run your program by declaring the variable as an integer rather than as a variant. An integer variable will speed up arithmetic operations too , thus gaining speed.


DATA TYPES ďƒ˜ To save time, you can use declaration characters to specify the data type.

Thus, Is the same as

Dim Name$ Dim Name As String


DATA TYPES  CONSTANTS Constant is a value that will never change. In programming VB 6.0, a constant will be declared by using keyword Constant (Const). Const Pi = 3.1415926 Where constant Pi is declare as 3.1415926 and a replacement constant π in program code when the constant π is needed.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.