CSCI 3327 Visual Basic Chapter 6 More Examples
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
Example 6. 3: Wage. Calculator. vb • URL: – http: //media. pearsoncmg. com/ph/esm/deitel/vb_ht p_2010/codeexamples. html • Subroutine: – Method that does not return a value • Sub method. Name (parameter-list): method header • Method body – block • Keyword Const: Declares a constant 3
Example 6. 4: Wage. Calculator. vb • URL: – http: //media. pearsoncmg. com/ph/esm/deitel/vb_ht p_2010/codeexamples. html • Function – Method that returns a value • Function method. Name(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 – result. Label. 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. To. Int 32 (…) Convert. To. Double (…) Convert. To. Decimal (…) Convert. To. String (…) 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: Fund. Raiser. vb • URL: – http: //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, total. Raised: class scope – Local variables in method donate. Button_Click: method scope 13
Random Class/Object • Random Object, class Random – Dim random. Object As New Random() – Dim random. Number As Integer random. Object. Next() = • random. Number is within [0, Int 32. Max. Value) – Dim Val As Integer = 1 + random. Object. Next(6) • Val is within [1, 6] – Dim Val As Integer = random. Object. Next(1, 7) • Val is within [1, 6] 14
Example 6. 11: Roll. Dice. vb • URL: – http: //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 • roll. Button. Enabled = False – Picture. Box • point. Die 1 Picture. Box. Image = Nothing 15
- Slides: 15