language elements 1. data types 2 floating point (real) single precision double precision decimal...

Post on 30-Dec-2015

220 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

LanguageElements

1

Data Types

2

Floating Point (real)Single Precision

Double Precision

Decimal

Fixed Point (integer)Byte

Short

Integer

Long

Numerical

3

Type Bytes Digits Range

Numerical

See page 105 in the text for additional numerical data types.

Double 8 14-15 ±1.8x10-324 to ± 4.9x10308

Single 4 6-7 ±1.4x10-45 to ±3.4x1038

Long 8 19-20 -92,233,720,368,547,755,808 to 92,233,720,368,547,755,807

Byte 1 2-3 0 to 255

Short 2 4-5 -32,768 to 32,767

Integer 4 9-10 -2,147,483,648 to 2,147,483,647

Decimal 16 28-29 ± 7.9x1024

4

TextualType Bytes Character Range

Char 2 1 (Unicode)

Variable depends on Length platform 0 to approx. 2 billion String

There are also fixed length Strings which we will examine later.

ASCII code is in the first 256 characters of Unicode

5

Logical

Type Bytes Range

Boolean 2 True or False

6

Type Bytes Range

Other

Date/ 8 midnight Jan. 1, 0001 Time to Dec. 31, 9999 Object 4 or 8 any OBJECT reference

User- varies Depends on user Defined

7

Identifiers

8

Non-Object NamingMust begin with a letter.

Must contain only letters, numbers, and the underscore character (_).

Punctuation characters and spaces are not allowed.

Must be no longer than 255 characters

9

VariablesFirst letter is lowercase and first letter of each word is uppercase - firstName

ConstantsFirst letter of each word is uppercase - NoOfStudents

Functions/SubroutinesBegin with a verb and first letter of each word is uppercase - FindStudentRecord

10

Non-Object Naming

Variables

11

Single

Double

Decimal

Byte

Integer

Long

String

Object

Boolean

Date

Dim/Public/Private … As

Private age As Integer

12

Dim valid As Booleanvalid = False

Public streetAddress As StringDim valid As Boolean = False

13

ArraysYou have had experience working

with arrays of simple and structured data in previous courses, so my only

comment is that Basic uses ( ) to delimit the subscripts and not [ ] that

you may have been using.

13

Dim age(100) As Integer

Creates an array of integers

Age(0) – Age(100)

Dim age(99) …Age(0) – Age(99)

99Dim grades(24,9) As Single

10 columns

25 r

ow

s

0 90

24

In VB, most indexedObjects begin with 0In VB, most indexedObjects begin with 0

Dim names() As String

Although Visual Basic does not require variable declarations, it is a moot point, since I require that you declare all variables by placing the Option Explicit statement in the first line of your code.

Option Explicit On

See page 107 in thetext for Dim variations.

14

In addition to local and global variables, VB (like c and c++) has an additional one called .

15

Private Sub cmdDIM_Click(…)… Dim x As Integer x = x + 1 lblDim.Text = x End Sub

Private Sub cmdStatic_Click(…)… Static x As Integer x = x + 1 lblStatic.Text = x End Sub

x shouldbe initialized

x shouldbe initialized

16

Run Program

View Code

Constants

17

Const MaxStudents As Integer = 100

Public Const State As String = “New York”

Constants are declared in the following ways.

18

Operators

19

^ power operator: 2^3 = 8

\ integer division: 26\4 = 6 (whereas 26/4 = 6.5)

Mod modulus (remainder): 26 Mod 4 = 2

Arithmetic OperatorsThe standard operators that are found in most computer languages are also found in VB. The only three you may not have seen are:

20

For a list of additional arithmeticoperators, see page 109 in the text.

Although VB allows shortcut assignment operators like

total += 100we will NOT use them!

Instead we will use “longhand”total = total + 100

Arithmetic Operators

21

VB allows you to concatenate (put together) strings with the string concatenation operator &.

String Operators

22

"cat" & "nip" = "catnip”

"12" & "34" = ____

(Note: some languages, including VB, use the + to indicateConcatenation. How would the following be interpreted?)

"cat" + "nip" = "12" + "34" = "12" + 34 = "12" & 34 =

(Note: some languages, including VB, use the + to indicateConcatenation. How would the following be interpreted?)

"cat" + "nip" = "12" + "34" = "12" + 34 = "12" & 34 =

For a list of additional relationaloperators, see page 147 in the text.

Relational Operators

The standard operators that are found in most computer languages are also found in VB:

=<>><

>=<=

23

The last two are new to VB.Netand perform “short-circuiting.”

Logical Operators

The standard operators that are found in most computer languages are also found in VB:

AndOrNot

AndAlsoOrElse

24

Unlike c++, Visual Basic does automatic casting (converting mixed data types). However, I require that you disable this by placing the Option Strict statement in the second line of your code.

Option Explicit OnOption Strict On

As shown in the text, you can usefunctions or method to make theconversions.

25

Dim age As Integer age = CInt(txtAge.Text)

You can use casting in:

Assignment - Dim x As Decimal x = Convert.ToDecimal(txtSalary.Text)

In context - If (CInt(txtCount.Text) < 10) Then …

Microsoft Help - Conversion Functions

Microsoft Help - Conversion Methods 26

Dim statements - Dim age As Integer = CInt(txtAge.Text)

Subroutines&

Functions27

1. You understand the difference between passing parameters by value (copy of the data) and reference (address of the data).

2. You know the difference between “formal” parameters (must be variables) and “actual” parameters (can be expressions).

3. You understand the concept of “scope.”

4. You know the difference between local and global variables.

28

Assumptions:

5. You know the difference between a subroutine (sub) procedure and a function procedure.

6. You know the difference between calling a subroutine and calling a function.

7. You know when/why you should use a subroutine/function.

29

Assumptions:

Therefore, we will focus on how subroutinesand functions are implemented in VB!

30

[Private | Public] Sub ProcedureName ([parameterlist]) statements[Exit Sub] [statements]End Sub

Subroutines

Parameterlist Syntax:

[ByVal | By Ref] variableName As type

DefaultDefaultSubroutine Call:

[Call] [Me.] ProcedureName ([argumentList])

Public Sub ClearTextBoxes() txtPrin.Text = “” txtRate.Text = “” txtTime.Text = “”End Sub

31

...ClearTextBoxes()...

See pages 173 & 175 in the text for additional examples of defining and calling subroutines.

Subroutines

32

[Private | Public] Function FunctionName ([parameterlist]) [As type] statements[Exit Function][Return expression] [statements]End Function

Functions

Function Call:

variable = [Me.] FunctionName ([argumentList])

Option Strict OnREQUIRES this!

Option Strict OnREQUIRES this!

Actually as long as FunctionName ([argumentList]) isused properly in “context,” a function call is initiated.Actually as long as FunctionName ([argumentList]) isused properly in “context,” a function call is initiated.

Private Function FindInterest( ByVal p As Decimal, ByVal r As Decimal, ByVal t As Decimal)

FindInterest = p*r/100D*t

End Function

33

...If(FindInterest(principal,rate,3D)< 1000D) Then ...... See pages 178 - 180 in the text for additional

examples of defining and calling functions.

Functions

Principal and ratewould be dimensioned as?

Principal and ratewould be dimensioned as?

As Decimal

Return p*r/100D*t

Programming ConstructsSequence

Repetition

Selection

34

SelectionConditional Statements

35

If statement(s)If (cond) Then S1 If (Rate > 65) Then FineMsg = "You owe $75.00"

If (cond) Then S1 Else S2 If (Grade < 60) Then lblmsg.Text = "failing" Else _ lblMsg.Text = "passing"

If (cond) Then statement(s)[Else statement(s)]End If [ ] denote optional statements

36

Unlike c/c++ and Java, no { } areneeded to delimit a blockof statements! The Then,

Else, or EndIf are the delimiters.

Unlike c/c++ and Java, no { } areneeded to delimit a blockof statements! The Then,

Else, or EndIf are the delimiters.

Using multiple If statements, write the code that would implement the following grading scale:

100 - 90 A 89 - 80 B

79 - 70 C 69 - 60 D

59 - 0 E

(Assume that the variables NGrade and LGradehave been appropriately declared. How is this done,

and what types would have been used?)37

ElseIf statement If (cond1) Then statement(s) ElseIf (cond2) Then statement(s) ElseIf (cond3) ... [Else statement(s)] End If

Note: ElseIf is one word but End Ifare two words like End Sub and End Class. If you mistype either, VB will automatically correct it for you!!!

Note: ElseIf is one word but End Ifare two words like End Sub and End Class. If you mistype either, VB will automatically correct it for you!!!

38

Select Case statement

Select Case var (or property) Case expression1

statement(s) Case expression2 statement(s) [Case Else statement(s)]

End Select

39

In the next example, we are using the same button, which can have one of two “texts”. This is similar to what is done in the iTunes player, except the button has graphics instead of text. The Play button changes from to the Pause button . Depending on the graphic, a different action is performed when the button is clicked.

40

Now using the Select Case statement, we will determine what action to take, based on the text of a specific command button.

41

Select Case cmdButton.Text Case "Start" statement(s) cmdButton.Text = "Stop" Case "Stop"

statement(s) cmdButton.Text = "Start"End Select

The code would be:

Be careful, eventhough VB is not case sensitive, strings are!!!

Be careful, eventhough VB is not case sensitive, strings are!!!

42

RepetitionLooping Statements

43

Looping Statements

Enumerated

Pre-test

Post-test

44

For var = beg To end [Step step]

statement(s)

Next var

Enumerated

For Counter = 1 To 10 . . .

Next Counter45

If we were indexingan array, we would

do: 0 To 9

If we were indexingan array, we would

do: 0 To 9

Do While (cond) statement(s) Loop

Pre-test

Do While (Len(txtLastName.Text) = 0)

. . .

Loop46

What is this testing?What is this testing?

You could also test:txtLastName.Text = “”You could also test:txtLastName.Text = “”

Post-test

Do Until (Temperature < 0) . . .

Loop

47

Do Until (cond) statement(s) Loop

Read the

Description

Of Assigment-2

48

top related