CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

15
CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 6: More Chapter 6: More Examples of Methods Examples of Methods UTPA – Fall 2011

Transcript of CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Page 1: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

CSCI 3327 Visual Basic CSCI 3327 Visual Basic Chapter 6: More Examples of Chapter 6: More Examples of

MethodsMethods

UTPA – Fall 2011

Page 2: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Objectives

• In this chapter, you will:– See more examples about methods

• Subroutines

• Functions

– Learn the implicit/explicit argument conversions– Become aware of the scope of the declaration– Get familiar with the Random class/object to

generate random numbers

2

Page 3: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Example 6.3: WageCalculator.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

• Subroutine:– Method that does not return a value

• Sub methodName (parameter-list): method header

• Method body – block

• Keyword Const: Declares a constant

3

Page 4: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Example 6.4: WageCalculator.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html

• Function– Method that returns a value

• Function methodName(parameter-list) As return-type

• Return statement: Return variable

4

Page 5: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Implicit Argument Conversions

• Widening conversion– An argument is converted to a parameter of

another data type that can hold more data

• Narrowing conversion– Opposite– Potential data loss during the conversion

5

Page 6: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Examples of Implicit Conversion

• Examples– Char String– Integer Decimal or Double – Decimal Integer– resultLabel.Text = Math.Sqrt(4)

6

Page 7: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Option Strict and Data-Type Conversions

• Visual Studio– 2010: Tools Options Projects and Solutions

VB defaults– 2008: Project Properties Compile

7

Page 8: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

8

Page 9: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Option Strict and Data-Type Conversions (cont'd)

• Option Explicit– On by default – declare all variables before they

are used

• Option Strict– Off by default– On – requires performing explicit conversions by

cast operator

9

Page 10: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Option Strict and Data-Type Conversions (cont'd)

• Option Infer– On by default – complier infers a variable’s type

based on its initializer value• Dim x = 7 ' compiler infers: x is Integer type

10

Page 11: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Explicit Conversion

• Explicit conversion– Class Convert

• Convert.ToInt32 (…)

• Convert.ToDouble (…)

• Convert.ToDecimal (…)

• Convert.ToString (…)

11

Page 12: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

The Scope of Declarations

• Block scope– A variable declared in the body of a control

statement– E.g., If … Then

• Method scope– A variable declared in a method

• Class scope– A member declared in the class’s body, but outside

the bodies of the class’s methods

12

Page 13: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Example 6.9: FundRaiser.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/

vb_htp_2010/codeexamples.html• Donation

– 17% of the donation is used for covering operating expenses

– Calculate the donation after expenses and the total raised

– Instance variable, totalRaised: class scope– Local variables in method donateButton_Click:

method scope

13

Page 14: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Random Class/Object

• Random Object, class Random– Dim randomObject As New Random()– Dim randomNumber As Integer =

randomObject.Next()• randomNumber is within [0, Int32.MaxValue)

– Dim Val As Integer = 1 + randomObject.Next(6)• Val is within [1, 6]

– Dim Val As Integer = randomObject.Next(1, 7)• Val is within [1, 6]

14

Page 15: CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.

Example 6.11: RollDice.vb

• URL:– http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/

codeexamples.html• Rules:

– First throw: 7, 11 – win; 2, 3, 12 – lose; 4, 5, 6, 8, 9, 10 – player's points

– Afterwards, 7 – lose; make their points – win • Controls

– Button• rollButton.Enabled = False

– PictureBox• pointDie1PictureBox.Image = Nothing

15