90 likes | 202 Vues
In this advanced programming lesson, we delve into the concept of objects, essential for defining and interacting with data within your programs. You'll learn how to create instances of classes, understand properties, and utilize methods. Step-by-step exercises will guide you through creating multiple objects like Zoe, Jay, and Joe using the `clsPerson` class. By the end of this session, you'll be proficient in accessing and manipulating object properties and methods, laying the groundwork for future topics in object-oriented programming.
E N D
CTEC2902Advanced Programming Creating & Using Objects
CTEC2902Advanced Programming • The Story So Far • You have been… • Doing review exercises in labs & elsewhere • Revising year 1 material (essential) • Let us carry on…
What is an Object? Any thing you can define and interact with in your program To create objects … Dim object_name As New class_name e.g., Dim dtSales As New DataTable You can now use dtSales to process data, using all features of DataTable
Creating & Using Objects Question: What service does a class provide? How do I create instances of the class? What are its properties and methods? How can I use its services? Answer: you need to read the API of the class
clsPerson Class Exercises • Create 3 objects, named Zoe, Jay, and Joe • Dim Zoe As New clsPerson • Dim Jay As New clsPerson • Dim Joe As New clsPerson
Properties To refer to a property Object.property_name Remember: properties behave like variables So, set or get values • e.g. to set values • Zoe.FirstName = “Zoe” • Zoe.Surname = “Smith” • Jay.DoB = #01/04/1995# • Joe.Gender = “M” e.g. to get values txtName.Text = Zoe.Surname lblBirthDate.Text = Zoe.Dob e.g. also … Zoe.DoB = Jay.DoB Joe.Gender = Jay.Gender
Methods Remember: methods behave like functions So, consider parameters To refer to a method Object.method_name() • e.g. Show Zoe’s age on screen • lblAge.Text = Zoe.Age() • txtName.Text = Jay.FullName()
Essential Revision • In your own time ... • Review these slides • Make sure you understand properties and methods
More on objects next week