Visual Basic Code Examples - Vasile Avram

Visual Basic - Commented Samples

1.1 Interchange Example 1.2 Defining Records Example 1.3 Using InputBox Function Example 1.4 Using MsgBox Function Example 1.5 Finding Min and Max From Two Values First Example 1.6 Finding Min and Max From Two Values Second Example 1.7 Finding Factorial Example 1.8 Finding Min and Max From Two Values Third Example 1.9 Ordering Values Example 1.10 Using If…Then…ElseIf Example 1.11 Extracting Square Root Example 1.12 Determining The Factorial Second Example 1.13 Determining The Easter Date For a Wanted Year Example 1.14 Determining The Factorial Third Example 1.15 Ordering Second Example 1.16 Using For Examples 1.17 Fahrenheit To Celsius Correspondence Table Example 1.19 Working With Matrix Example

COPYRIGHT© 2006-2009 Vasile AVRAM.

1.1 Interchange Example

This algorithm realize an interchange of the content of the variable named x with the content of the variable y. The variables x and y from the name of the procedure Exchange(x,y) are named arguments and the real value for that are passed to the procedure at call time. The call is realized, in almost algorithmic programming languages (as C ++, Visual Basic, Pascal etc.) by writing the name of the procedure followed by the list of the arguments. The call Exchange(x, y) means applying the operations included in the procedure body to variables x and y.In the computation block from the figure the numbered line means:

A flowchart with an interchange operation Figure 1 The representation of Exchange operation

- (1) the content of variable x is stored in a temporary (working) variable named temp; - (2) the content of variable x is overwritten by the content of variable y (first is deleted and after deletion the content of y is copied y); - (3) the content of y is overwritten by those of the variable temp (it contains the value of x as passed at the call time. The new values of x and y are returned to the caller.

For example, the call Exchange(3, 2) realizes the operations:

temp = 3

x = 2

y = 3 (the temp content)

And returns 2, 3

top

1.2 Defining Records Example

The user defined data type is formed by placing the needed declarative sentences Type block. For example, if we want represent the structure of a row from the Balance_Sheet (the Romanian one) this can be declared by the user as follows:

Type Balance_Sheet_Row Dim Account_ID As String*22 Dim Account_Description As String*60 Dim Db_Jan As Double Dim Cr_Jan As Double Dim Db_Prev As Double Dim Cr_Prev As Double Dim Db_Month As Double Dim Cr_ Month As Double End Type After declaration a user-defined data type can be used in the same way data type used. For our example, we can define a memory variable, that we call Current_Row, thus: Dim Current_Row As Balance_Sheet_Row. You can find other examples of declaring and using user data types (including in other user data type) in: Open with Adobe Reader Visual Basic Code Examples

top

1.3 Using InputBox Function Example

The call InputBox(“Prompt”,”Valoare_implicita”,”Titlu”) will produces the dialog box from figure 2.

InputBox - the dialog window structure Figure 2 Example of using InputBox

top

1.4 Using MsgBox Function Example

For example the call MsgBox(“Prompt”,vbInformation+vbOkCancel, “Titlu”) will produces the dialog shown in figure 3.

The returned values correspond to the pressed button:

MsgBox - the dialog window structure Figure 3 Example of an MsgBox dialog

Constant

Value

Description

vbOK

1

OK

vbCancel

2

Cancel

vbAbort

3

Abort

vbRetry

4

Retry

vbIgnore

5

Ignore

vbYes

6

Yes

vbNo

7

No

In the following example is illustrated how to write the message on many lines and what means a string expression: MsgBox "The Student " & Name & Chr(13) & Chr(10) & " has the average mark: " & media What is between “…” means literal strings that will be placed as such in the message; Name and media are named variables whose content will be concatenated by the string concatenation operator (&) with the literals and the function calls Chr(13) & Chr(10) means display what follows on a separate line.

top

1.5 Finding Min and Max From Two Values First Example

Two ways to reprezent graphically the algorithms for determining Min and Max from two values

a) b) Figure 3 A description of min and max algorithms for any kind of data type values

Từ khóa » Visual Basic Flowchart Examples