visual basic programming i 56:150 information system design
Post on 21-Jan-2016
214 Views
Preview:
TRANSCRIPT
Visual Basic Programming I
56:150 Information System Design
IntroductionVisual Basic has evolved from the original BASIC language and now contains several hundred statements, functions, and keywords, many of which relate directly to the Windows GUI
The Visual Basic programming system, Applications Edition included in Microsoft Excel, Microsoft Access, and many other Windows applications uses the same language. The investment you make in learning Visual Basic will carry over to these other areas.
Data Type• Byte 1-byte (0-255)
•Boolean 2-byte (True or False)
•Integer 2-byte (-32,768 to 32767)
•Long 4-byte (-2,147,483,648 to 2,147,483,647)
•Date 8-byte (Jan. 1, 100 to Dec. 31, 9999)
•String 0 – 65535 bytes
Data Type• Single 4-byte (Single-precision floating-point value)
• Double 8-byte (Double-precision floating-point value)
•Currency 8-byte (Currency value)
•Decimal Decimal value
•Variant capable of storing all system-defined types of data
Variant data can contain a special value “Null”
•object types
Declare Variables
To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable:
Dim variablename [As type] if type is not specified, it will be variant.
“dim” could be replaced by static, private, public for different purposes.
Variable Name:Must begin with a letter. Case Insensitive.
Can't contain an embedded period or embedded type-declaration character.
Must not exceed 255 characters.
Must be unique within the same scope, which is the range from which the variable can be referenced — a procedure, a form, and so on.
Implicit vs. Explicit Declaration
Function SafeSqr(num) TempVal = Abs(num) SafeSqr = Sqr(TempVal)
End Function
What if you write the second Tempval as Temval mistakenly?Option Explicit
Scope of Variables
Scope Private Public
Procedure-level
Variables are private to the procedure in which they appear.
Not applicable
Module-level
Variables are private to the module in which they appear.
Variables are available to all modules.
Constants
[Public|Private] Const constantname[As type] = expression
Const conPi = 3.14159265358979Const conMaxPlanets As Integer = 9
Const conPi = 3.14, conMaxPlanets = 9, _ conWorldPop = 6E+09 Const conPi2 = conPi * 2
Define fixed-size array
Follow the array name by the upper bound in parenthesis. The default lower bound is 0.
Dim Sums(20) As DoublePublic Counters(14) As Integer
The lower and upper bound can both be determined
Dim Sums(100 To 120) As String
Dynamic Array
Dim DynArray()
ReDim Matrix1(19, 29) ’multidimensional ’array
A dynamic array can be resized at any time.
“Preserve” can be used to keep the contents while enlarging a dynamic array.
(ReDim Preserve arrayname(dimensions)
Operators
Arithmetic operators +, –, *, /, mod \ integer division operator ^Exponent operator
Comparison Operators =, < >, >, < , <=, >= is result = obj1 is obj2
Operators
Logical operators Not, And, Or, Xor
Concatenation operators & To connect two strings.
Debug. Print “This” & “is” & “a” & “test”
Other Symbols
’ to make comments
_ to continue a line.
Decision Structure
If…ThenIf condition Then statementIf condition Then
statements End If
Decision Structure
If…Then…ElseIf condition1 Then
[statementblock-1][ElseIf condition2 Then
[statementblock-2]] ...[Else
[statementblock-n]] End If
Decision StructureSelect Case
Select Case testExpression[Case expressionlist1
[statementblock-1]][Case expressionlist2
[statementblock-2]].[Case Else
[statementblock-n]] End Select
Case else is optional
Loop Structure
Do...LoopDo (While | Until) condition
statementsLoop
Do statements
Loop (While | Until) condition
Loop Structure
For...NextFor counter = start To end [Step increment]
statementsNext [counter]
Sub Procedure
A Sub procedure is a block of code that is executed in response to an event.[Private|Public][Static] Sub procedurename (arguments)
statements
End Sub
The arguments for a procedure are like a variable declaration, declaring values that are passed in from the calling procedure.
Function Procedure
Unlike a Sub procedure, a Function procedure can return a value to the calling procedure
[Private|Public][Static]Function procedurename (arguments) [As type]
statementsEnd Function
Function ProcedureFunction procedures have data types, just as variables do. This determines the type of the return value. Return a value by assigning it to the procedurename itself. Function Hypotenuse (A As Integer, B As _ Integer) As String
Hypotenuse = Sqr(A ^ 2 + B ^ 2)
End Function
Passing arguments by valueOnly a copy of a variable is passed when an argument is passed by value. If the procedure changes the value, the change affects only the copy and not the variable itself. Use the ByVal keyword to indicate an argument passed by value.
Sub PostAccounts(ByVal intAcctNum as Integer)
statements End Sub
Passing arguments by reference
the variable's value can be permanently changed by the procedure to which it is passed.
Passing by reference is the default in Visual Basic.
References
http://msdn.microsoft.com/library/default.asp
http://graphicsmagician.com/vbcourse/index.htm
top related