0% found this document useful (0 votes)
15 views

Lecture 02a - Objects and Classes

Uploaded by

b748733
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 02a - Objects and Classes

Uploaded by

b748733
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2023/07/31

Graphical User Interface; Objects and Classes


Lecturer:
Ms Marinda Taljaard / Mr Ighsaan Salie / Mr Ntembeko Jafta

Working with Forms


▪ The form is an object and every form control you put on the form is an object
▪ Objects have an identifier, and properties that can be
• Assigned a value by using the property panel
• Assigned a value when writing the code – in the code
• Assigned a value from user input
• Updated during the running of the code

▪ Some examples:
• txtAge.Text = …
• lblOutput.Text = …
• btnCalculate.BackColor = …

1
2023/07/31

Working with Forms and objects


▪ At this stage, since we add our code to the button click event, changes specified in code
will only happen once a button is clicked
▪ It is possible to have more than one button – each with its own functionality

▪ Use dot syntax (identifier.Property) to obtain/set the value for a property (e.g.
txtAge.Text)

First homework exercise discussion


▪ Follow the steps provided in the slides and re-create this simple GUI applications yourself
▪ Make your code easier to read and understand than the one on the slides, by changing
default form control names, e.g.
• lblHeading
• lblDisplayText
• txtDisplayText
• lblQty
• txtQty
• btnContinue
• lblOutput

2
2023/07/31

Second homework exercise discussion


▪ Write a windows application for the following scenario:
A person qualifies for a subsidy if he is older than 50 and earns a monthly salary
less than R2000.00. The subsidy is calculated as 15% of the monthly salary.
Your application must read in a person’s age and salary and then display a
message similar to one of the following messages (for different input):

Your salary is R1500.00 and you receive a subsidy of R225.00


Your salary is R2500 and you do not receive a subsidy
You are younger than 50 and you do not receive a subsidy

Homework – example form design


▪ Form controls renamed to the following:
• lblHeading
• lblAge
• txtAge
• lblSalary
• txtSalary
• btnCalculate
• lblOutput (not visible, as initially empty)

▪ Naming convention helps differentiate between the label and textBox (e.g. for Age)

3
2023/07/31

Formatting output in GUI applications


▪ There are different ways you can build up output messages to assign to the Text property
of a label, some examples:
▪ Output message can be built up using concatenation
• message = message + "\n" + txtDisplayText.Text;
• labelOutput.Text = message;

▪ Output message can also be done using String.Format()


• labelOutput.Text = String.Format("Your salary is {0:C} and you do not
receive a subsidy", salary)
▪ Currency formatting can also be done using: variable.ToString(“C”)
• labelOutput.Text = "Your salary is " + salary.ToString("C") + " and you
receive a subsidy of " + subsidy.ToString("C");

Homework – one possible solution


▪ A person qualifies for a subsidy if he is older than
50 and earns a monthly salary less than
R2000.00. The subsidy is calculated as 15% of
the monthly salary.

Different output methods used as an example –


definitely not required to use different methods

4
2023/07/31

Adjusted homework exercise


▪ Change your solution to allow for the subsidy calculation at 10% or at 15%. The
user needs to specify the appropriate subsidy percentage.
A person qualifies for a subsidy if he is older than 50 and earns a monthly salary
less than R2000.00. The subsidy is calculated as 15% of the monthly salary.
Your application must read in a person’s age and salary and then display a
message similar to one of the following messages:

Your salary is R1500.00 and you receive a subsidy of R225.00


Your salary is R2500 and you do not receive a subsidy
You are younger than 50 and you do not receive a subsidy

Adjusted homework exercise


▪ Add radio buttons to the form, e.g.
• rbnSub1
• rbnSub2
▪ A radio button can be pre-selected when the form is created
▪ Use an if to determine which subsidy percentage should be used

5
2023/07/31

Adjusted homework exercise


▪ Two new constants declared
▪ Another if now required – check the condition of the if

GUI vs Console applications


▪ Check the question/task to ensure you give the appropriate solution
▪ Focus this semester more on programming structures and algorithms
▪ Mostly practiced in the console environment

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

▪ Features of Object-Oriented Programming languages


• Classes (covered in this module)
• Objects (covered in this module)
• Encapsulation (covered in WRAV201)
• Inheritance (covered in WRAV201)
• Polymorphism (covered in WRAV201)

C# classes and objects


▪ Working with classes and objects in C# has many aspects to consider
• Why do we need them?
• What are they?
• How do we create and use them?
▪ Introduction today
▪ More detail and practice in next session

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)

▪ Some new terminology:


• Instantiate – to create a tangible example
• An object is an instantiation of a class or an instance of a class
- We create the class first, then we create objects of that class

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

▪ Next the attributes are created, and can be assigned values


▪ Use dot syntax to access the attributes of an object (Player1.name)

9
2023/07/31

Creating a class in Visual studio


▪ Create/open the application you want to work on
▪ Right-click on the application name in solution explorer, Add, Class

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

Module to-do list


▪ Locate prac 1 instructions on Moodle – start working on the prac tasks

▪ Work through the videos available on Moodle


• Classes and Objects – Introduction
• Defining classes in Visual Studio
• Constructor

▪ 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

You might also like