Presentation is loading. Please wait.

Presentation is loading. Please wait.

PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.

Similar presentations


Presentation on theme: "PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008."— Presentation transcript:

1 PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008

2 2 © Dr. Herbert G. Mayer Agenda Introduction Types Empty Program Scopes Variable Objects Constants Tables Procedures, AKA Sub

3 3 © Dr. Herbert G. Mayer Introduction Program starts, performs actions on objects, some of which vary, others are constant, all have some type, actions are grouped into procedures Procedures in VB –Event Procedures, created by IDE Integrated Development Environment –General procedures AKA subroutines, keyword Sub, created by you –Also Functions, similar to procedures, but return value –Other languages: Subroutines Start execution at Event procedures (sub) whose button is pushed 1 st Actions expressed in statements of sub –Sometimes actions abbreviated in declarations: e.g. Initialization Actions manipulate objects, can use constant or variable; action can be: I/O (Print, Read, Write), arithmetic or boolean operation, comparison, use as actual parameter Objects have type, e.g. –Integersmall range of integral values: -2147483647 –CharASCII characters: ‘x’, Chr( 0 ) ASCII literals not in VB –String“hello”, “How are you”, “Caesar’s ““De Bello Gallico””” –Double3.1415927, 0.5, 0.0, 45.6

4 4 © Dr. Herbert G. Mayer Types Def: A type defines a domain of values of a specific class; and each object has a type and (generally) a name Objects of any type consume physical memory in a computers of a particular size, to hold their values E.g. char objects consume exactly 1 byte each of storage on a byte-addressable architecture, to hold value ‘x’ for example Integer type objects consume 4 bytes on some architectures, to hold values, e.g.: -2,147,483,648 –Commas, added for ease of readability, not part of VB number Some type conversions are implied and easy: integer -> string

5 5 © Dr. Herbert G. Mayer Empty Program Class Form1:generated by Form1 of VB Button1_Click:generated by 1 st button sender:1 st formal value-parameter e:2 nd parameter Parameters of event procedures not discussed; will discuss user-written procedures and their parameters (formal and actual) Public Class Form1 makes Form1 known publicly, i.e. can be activated by OS End Class terminates source program ByVal parameter doesn’t ever change the actual parameter! Public Class Form1 Private Sub Button1_Click( _ ‘ this is a line continuation ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click ‘ nothing else – this is a comment only End Sub End Class

6 6 © Dr. Herbert G. Mayer Scopes Def: Scope is a domain of program text, over which a name is defined; i.e. outside scope such a name is not known VB has 3 kinds of scope: –Procedure scope starts at reserved keyword Sub and ends at reserved keywords End Sub –Class scope starts at reserved keyword Class and ends at reserved keywords End Class and is not qualified as Public. –Program scope covers all Forms of a complete program. Such names are defined inside all classes associated with all Forms VB allows forward referencing

7 7 © Dr. Herbert G. Mayer Variable Objects Named objects of some type and value that can be modified are known as variables, AKA objects Assignment to variable on rhs of assignment, or by use as actual for reference parameter in a call Variables and constants can enter into operations with one another; e.g. v1 = v2 + c1 * v3 Example: Dim gpa As Double = 3.485 ‘ init.

8 8 © Dr. Herbert G. Mayer Constants Named objects of some type and value that cannot be modified, are known as constants, AKA constant objects Declared like variable, using Const keyword Note that unnamed constants exist as well, they are known as literals, such as 12345, or “ hello ” Constants cannot be used on rhs of Assignment Statement, nor as actual for formal ref-parameter Preferable to use named constants –Gives meaning to an otherwise anonymous literal –Allows change in 1 place when used n times Example: Const PI As Double = 3.1415927 Predefined constants, example: vbNewline

9 9 © Dr. Herbert G. Mayer Tables Def: A Table of n elements of some type has n instances of (elements of) the same type under 1 name, each distinguished by index In other languages called: Array Low-bound in VB is 0 -- like in C++ Table looks like a scalar variable, but may have multiple instances; may also have just 1 index Example1: Dim score( 10 ) As Double Example2: Dim name1( 5 ) As String Example3: Dim name2( ) As String = { “Jo”, “Jim”, “Jack”, “Jo” } ‘ has 4 elements

10 10 © Dr. Herbert G. Mayer Procedures, AKA Sub Syntax like event procedures that are defined automatically in VB But written by user, with user-defined parameters, if any Can be called, to be activated like any other statement May have formal parameters Requires that call provide matching actual parameters ByVal or ByRef formal parameters Actual and formal parameters need to be pair-wise type-compatible At place of call, Sub/procedure is invoked and returns to place after call Actions performed are defined inside procedure body: Statement List Procedure is known by its name, thus is callable In VB there are: Subs and Functions Example: Private Sub Swap( ByRef a As Integer, ByRef b As Integer ) Dim temp As Integer = b b = a a = temp End Sub ‘ Swap... Swap( x, y(i) ) ‘ a call statement


Download ppt "PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008."

Similar presentations


Ads by Google