Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011."— Presentation transcript:

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

2 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

3 Example 6.3: WageCalculator.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_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

4 Example 6.4: WageCalculator.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_ht p_2010/codeexamples.html Function –Method that returns a value Function methodName(parameter-list) As return-type Return statement: Return variable 4

5 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

6 Examples of Implicit Conversion Examples –Char  String –Integer  Decimal or Double –Decimal  Integer –resultLabel.Text = Math.Sqrt(4) 6

7 Option Strict and Data-Type Conversions Visual Studio –2010: Tools  Options  Projects and Solutions  VB defaults –2008: Project  Properties  Compile 7

8 8

9 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

10 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

11 Explicit Conversion Explicit conversion –Class Convert Convert.ToInt32 (…) Convert.ToDouble (…) Convert.ToDecimal (…) Convert.ToString (…) 11

12 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

13 Example 6.9: FundRaiser.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/codeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_20 10/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

14 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

15 Example 6.11: RollDice.vb URL: –http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/c odeexamples.htmlhttp://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/c odeexamples.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


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

Similar presentations


Ads by Google