Objects and Classes Creating your own class - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Objects and Classes Creating your own class

Description:

Initialises the random number generator based on the time. Randomize ... initialise random number generator. Randomize() ' Rnd generates a random number between ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 23
Provided by: itserv5
Category:

less

Transcript and Presenter's Notes

Title: Objects and Classes Creating your own class


1
Objects and ClassesCreating your own class
2
You should be able to
  • Understand how to
  • write a simple class.
  • use a class to create multiple objects.

3
Building our own Class
  • We will build a dice class.
  • It will have a face value property.
  • The dice will be able to roll, and this will
    change the face value.

4
Dice Class (CDice)
Class
Object (Instance of a Class)
  • Attributes
  • Face Value
  • Methods
  • Roll

CDice faceValue Integer roll
(CDice) 4
5
Creating the Dice Class
  • Let the program know we are creating a class.
  • Put code in a class module (Project gt Add Class).
  • Give the class a name this is the data type
    (i.e. Integer, String, CDice).
  • VB will add some code into the class file
  • Public Class CDice
  • End Class
  • Note Naming conventions
  • we usually start a class name with a capital C.

6
  • Declare the properties.
  • Public faceValue As Integer
  • Declare the procedures.
  • Public Sub roll()
  • End Sub
  • This procedure will alter the faceValue property
    of the dice object.
  • Notice the scope - Public

7
  • ' class CDice
  • Public Class Cdice
  • Public faceValue As Integer
  • Public Sub roll()
  • ' randomly change the faceValue
  • ' must be to an integer between 1 and 6
  • ' can be the same as the previous value
  • End Sub
  • End Class

8
Rolling the dice how?
  • Roll the dice,
  • i.e.
  • Choose a random number between 1 and 6.
  • Rnd
  • Generates a random number between 0 and 1
    (includes 0, but not 1).
  • Value Rnd(optional number)
  • We will use it without the optional number. Look
    at the help for the Rnd Function for more
    details.
  • Value Rnd()

9
Rolling the dice
  • Randoms in programming are not real randoms, but
    are generated from a pseudo random list.
  • We must choose where to start in this list...
  • Randomize
  • Initialises the random number generator based on
    the time.
  • Randomize()
  • Initialises the random number generator based on
    a seed number.
  • Randomize(optional number)

10
Rolling the dice
  • ' initialise random number generator
  • Randomize()
  • ' generates a random number between 0 1
  • Value Rnd()
  • But we want an integer between 1 6 (inclusive)!
  • Start by getting a number between 1 and 6...
  • Then convert this to an integer

11
An integer between 1 6
  • Change the range...
  • Value Rnd() between 0.00 and 0.99
  • 6Value between 0.00 and 5.99 (not including 6)
  • Change the start value (min)
  • 6Value 1 between 1.00 and 6.99
  • So...
  • NewValue 6Value 1
  • Then, convert to an integer
  • IntValue Int(NewValue)

12
An integer between 1 6
  • So...
  • ' initialise random number generator
  • Randomize()
  • ' generates a random number between 0 1
  • Value Rnd()
  • ' change the range
  • NewValue 6Value 1
  • ' convert to an integer
  • IntValue Int(NewValue)
  • OR...
  • Randomize()
  • ' generates a random number between 0 1
  • IntValue Int( 6 Rnd() 1 )

13
Rolling the dice
  • IntValue Int( 6 Rnd() 1 )
  • More generally...
  • Converting Random numbers
  • Convert the number to an integer between min and
    max inclusive
  • IntValueInt( (max-min 1)Rnd() min)
  • Think through this expression. What happens with
    min1 and max6? When Rnd() gives 0 and when
    Rnd() gives 0.99?

14
  • ' class CDice
  • Public Class Cdice
  • Public faceValue As Integer
  • Public Sub roll()
  • Dim Value As Double
  • Const min As Integer 1
  • Const max As Integer 6
  • ' initialise random number generator
  • Randomize()
  • ' Rnd generates a random number between 0 and
    1
  • Value Rnd()
  • ' convert random number to be an integer
  • ' between 1 and 6 inclusive
  • faceValue Int( (max-min 1)Value min)
  • End Sub

This is the Public variable declared above.
15
Using the Dice Class Declaring Creating Objects
  • Declare name for Object of a specific type/class
  • This declares a name to use to refer to the
    object.
  • Use Public, Dim etc. to get the right scope.
  • CDice should appear in the code complete window
    as a data type.
  • Public myDice As CDice
  • Create (instantiate) object of a specific
    type/class
  • use the New keyword
  • myDice New CDice
  • Note Naming conventions
  • we usually start object names with a lower case
    letter.

16
Using the Dice Class
  • We will create the dice object when the form is
    loaded.
  • When we click on a roll button we want the dice
    object to perform a roll action the face value
    should change.
  • We can then ask the dice object for the current
    face value and display this on the form.

17
Many Dice Form
cmdGo
lblDice2
lblDice3
lblDice1
18
  • Form code
  • Windows Form Designer generated code
  • ' declare myDice object of type (class) CDice
  • Public myDice1 As CDice
  • Private Sub Form1_Load(... ) Handles ...
  • ' create a new CDice object called myDice
  • myDice1 New CDice
  • End Sub
  • Private Sub cmdRoll_Click(... ) Handles ...
  • ' call the roll action of the myDice object
  • myDice1.roll()
  • ' display the faceValue property of myDice
  • lblDice1.Text CStr(myDice1.faceValue)
  • End Sub

19
Creating more than 1
  • We could create many objects of the type CDice,
    all would have the same properties and methods,
    but their own individual state.
  • Once the class has been written, we do not have
    to worry about how the methods are implemented,
    only how to use them.

20
Creating more than 1
  • Declare more then one object name and type
  • Public myDice1 As CDice
  • Public myDice2 As CDice
  • Create more than one object
  • myDice1 New CDice
  • myDice2 New CDice
  • Roll and display more than one Dice
  • ' call the roll action of the CDice objects
  • myDice1.roll()
  • myDice2.roll()
  • 'display the faceValue of the Cdice objects
  • lblDice1.Text CStr(myDice1.faceValue)
  • lblDice2.Text CStr(myDice2.faceValue)

21
Useful VB
  • Randomize
  • Initialises the random number generator based on
    the time.
  • Randomize
  • Rnd
  • Generates a random number between 0 and 1
    (includes 0, but not 1).
  • Number Rnd(optional number)
  • Converting Random numbers
  • Convert the number to an integer between min and
    max inclusive
  • IntNumber Int(Value (max - min 1) min)

22
You should be able to
  • Understand how to
  • write a simple class.
  • use a class to create multiple objects.
Write a Comment
User Comments (0)
About PowerShow.com