Download presentation
Presentation is loading. Please wait.
1
Using Classes1 Using Existing Classes Random CTEC2902 Advanced Programming
2
The story so far... You know What an object is What a class is The relationship between classes and objects Let’s carry on… CTEC2902 Advanced Programming 2Using Classes
3
3 How to Use An Existing Class Step 1 – Create an object instance of the class Dim instance_name As New class_name Name of class you want to use Your identifier for your object (meaningful name) Must already exist MUST have this keyword (called a constructor)
4
Using Classes4 The object Rambo has all the properties and methods of class Random How to Use An Existing Class Step 1 – Create an object instance of the class E.g. Dim Rambo As New Random butwhat is Random? What service does it provide? what are its properties and methods? how can we use its services? Answer: you need to read API of Random
5
Using Classes5 Application Programmers’ Interface An API must explain Service provided Namespace Class name Inherited classes Constructors Public Properties Public Methods
6
Using Classes6 Using Classes – Example(for lab exercises) VB.NET built-in random number generator Class name:Random Method name:Next (Min, Max) for integer values Returns a random value, where... Min <= Value < Max
7
Using Classes7 Example - Using Classes Set a variable to a random value in the range 0-9 ‘Create a random number-generating object Dim Generator As New Random ‘Create a variable to hold the random value Dim MyRandomDigit As Integer ‘Generate a random value & save it MyRandomDigit = Generator.Next (0, 10) Your variable now holds a value between 0 and 9, inclusive
8
Using Classes8 Exercise – Random Class Generate 2 random numbers, in the range 10-50, show the larger of the two in the text box txtBig Dim Fred As New Random Dim First As Integer, Second As Integer First = Fred.Next (10, 51) Second = Fred.Next (10, 51) If First > Second Then txtBig.Text = First Else txtBig.Text = Second End If
9
Using Classes9 Exercise – Random Class Generate 100 random integer values, between 0 and 500, and place them in a list box list, named lstFred D im Randomizer As New Random, Val As Integer Dim I As Integer For I = 1 To 100 Val = Randomizer.Next(0, 500 + 1) lstFred.Items.Add (Val) Next
10
Using Classes10 Exercise – Random Class Generate 1000 random integer values, between 0 and 100, and place them in an array list, named Bob Dim Bob As New ArrayList Dim Randomizer As New Random, Val As Integer Dim I As Integer For I = 1 To 1000 Val = Randomizer.Next(0, 100 + 1) Bob.Add (Val) Next
11
Using Classes11Exercises11 Hint: the best way to see what an object offers, in the way of properties and classes, is to type in a dot after object name and scroll the list box that appears as a result If the list box does not appear when you type in a dot, then something is wrong – your object is not recognised as an object by.NET
12
Using Classes12 Summary To create an object instance of a class Dim object_name As New class_name To set or get property values of an object a)To setobject_name.property_name = Source b)To getTarget = object_name.property_name To use methods of an object object_name.method_name (optional parameters) Note the full stops Object properties behave like variables; i.e., Have data types Store values Values may change Object methods behave like procedures; i.e., Each one performs an action May need parameters May return data
13
Using Classes13 Any questions? More objects & classes next week
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.