Lecture 02a - Objects and Classes
Lecture 02a - Objects and Classes
▪ Some examples:
• txtAge.Text = …
• lblOutput.Text = …
• btnCalculate.BackColor = …
1
2023/07/31
▪ Use dot syntax (identifier.Property) to obtain/set the value for a property (e.g.
txtAge.Text)
2
2023/07/31
▪ Naming convention helps differentiate between the label and textBox (e.g. for Age)
3
2023/07/31
4
2023/07/31
5
2023/07/31
6
2023/07/31
Objects in C#
▪ When working with forms (GUI interface), you have started using objects
▪ Objects are not only for forms – they are an essential part of object oriented
programming
7
2023/07/31
Scenario
▪ Write a console application named TestSoccerPlayer that instantiates and displays
a SoccerPlayer object. The SoccerPlayer class contains properties that hold a
player’s name (a string), jersey number (an integer), goals scored (an integer)
and assists (an integer)
• So we have: 4 items to store for each player
• But we do not know how many players?
• How will we store this?
• 4 parallel arrays?
▪ A better solution would be to use C# Classes and Objects (instead of 4 parallel
arrays)
Classes
▪ You can think of a class as a datatype
▪ To store 4 items for each soccer player, create a class
(new datatype) that combines those items
▪ A class header (or class definition) contains 3 parts:
• An optional access modifier
- public, protected, internal, private
- internal is the default, and perfect for us at this stage
• The keyword class
• Any legal identifier you choose for the name of your class (often a
singular noun)
▪ Specify the types and attributes of the new class
8
2023/07/31
Scenario
▪ Write a console application named TestSoccerPlayer that instantiates and displays
a SoccerPlayer object. The SoccerPlayer class contains properties that hold a
player’s name (a string), jersey number (an integer), goals scored (an integer)
and assists (an integer)
Objects
▪ Declaring a class does not create any actual objects – the class is an abstract description
of what the object will be like (when instantiated)
▪ If a class can be seen as a datatype, you can think of an object as a variable (of that
datatype), and the object can have a number of attributes (or fields), e.g.
• For each soccer player we have 4 attributes (name, number, goals, assists)
▪ When you declare an object
• C# is creating a reference (pointer) to space reserved in memory (for this object)
• Every object name is a reference to computer memory where the fields for the object resides
9
2023/07/31
Defining a Class
▪ In your newly created class file
• Specify the name of the class (no access modifier required – use the default value – internal)
• Specify the datatypes and identifiers for the required attributes (fields)
▪ Remember: this only describes what the object would look like – no object created yet
▪ Classes can also contain methods – more about this later
10
2023/07/31
▪ We will be discussing more aspects about this topic in the next session
▪ Link to additional online resource: https://www.w3schools.com/cs/cs_classes.php
Any questions
11