CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.
-
Upload
jonathan-hall -
Category
Documents
-
view
215 -
download
0
Transcript of 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
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
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
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
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
Examples of Implicit Conversion
• Examples– Char String– Integer Decimal or Double – Decimal Integer– resultLabel.Text = Math.Sqrt(4)
6
Option Strict and Data-Type Conversions
• Visual Studio– 2010: Tools Options Projects and Solutions
VB defaults– 2008: Project Properties Compile
7
8
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
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
Explicit Conversion
• Explicit conversion– Class Convert
• Convert.ToInt32 (…)
• Convert.ToDouble (…)
• Convert.ToDecimal (…)
• Convert.ToString (…)
11
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
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
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
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