OOP - Create and Use Classes
OOP - Create and Use Classes
Object-Oriented Programming
How to create
and use classes
C12, Slide 1
Objectives
Applied
1. Given the specifications for an app that uses classes, structures,
records, or record structs, develop the app.
Knowledge
1. List and describe the three layers of a three-layered application.
2. Describe these members of a class: constructor, method, and
property.
3. Describe the concept of encapsulation.
4. Explain how instantiation works.
5. Describe the main advantage of using object initializers.
6. Explain how auto-implemented properties work and how they
differ from properties that use a private field.
C12, Slide 2
1
4/16/2024
Objectives (continued)
7. Explain how expression-bodied methods, constructors, properties,
and accessors work.
8. Explain what a static member is.
9. Describe the concept of overloading a method or constructor.
10. Explain what a required property is.
11. Describe the use of property patterns with objects.
12. Describe the difference between a class, a structure, a record and a
record struct.
C12, Slide 3
C12, Slide 4
2
4/16/2024
// a public method
public string GetDisplayText(string sep) =>
$"{Code}{sep}{Price.ToString("c")}
{sep}{Description}";
}
}
C12, Slide 5
C12, Slide 6
3
4/16/2024
C12, Slide 7
C12, Slide 8
4
4/16/2024
C12, Slide 9
namespace ProductMaintenance
{
internal class Product
{
}
}
C12, Slide 10
10
5
4/16/2024
C12, Slide 11
11
C12, Slide 12
12
6
4/16/2024
C12, Slide 13
13
C12, Slide 14
14
7
4/16/2024
C12, Slide 15
15
C12, Slide 16
16
8
4/16/2024
C12, Slide 17
17
C12, Slide 18
18
9
4/16/2024
C12, Slide 19
19
C12, Slide 20
20
10
4/16/2024
C12, Slide 21
21
C12, Slide 22
22
11
4/16/2024
C12, Slide 23
23
C12, Slide 24
24
12
4/16/2024
C12, Slide 25
25
C12, Slide 26
26
13
4/16/2024
C12, Slide 27
27
C12, Slide 28
28
14
4/16/2024
if (errorMessage != "")
{
success = false;
MessageBox.Show(errorMessage, "Entry Error");
}
return success;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
C12, Slide 29
29
C12, Slide 30
30
15
4/16/2024
C12, Slide 31
31
C12, Slide 32
32
16
4/16/2024
C12, Slide 33
33
C12, Slide 34
34
17
4/16/2024
C12, Slide 35
35
C12, Slide 36
36
18
4/16/2024
[SetsRequiredMembers]
public Product(string code, string description,
decimal price)
{
Code = code;
Description = description;
Price = price;
}
C12, Slide 37
37
C12, Slide 38
38
19
4/16/2024
C12, Slide 39
39
C12, Slide 40
40
20
4/16/2024
C12, Slide 41
41
C12, Slide 42
42
21
4/16/2024
C12, Slide 43
43
A Product structure
public struct Product
{
public Product(string code, string description,
decimal price)
{
this.Code = code;
this.Description = description;
this.Price = price;
}
public string Code { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
C12, Slide 44
44
22
4/16/2024
C12, Slide 45
45
// Call a method
string msg = p.GetDisplayText("\n");
C12, Slide 46
46
23
4/16/2024
C12, Slide 47
47
p2.Price = 61.50m;
isEqual = p1.Equals(p2); // isEqual is false
C12, Slide 48
48
24
4/16/2024
C12, Slide 49
49
A Product record
public record Product
{
public Product(string code, string description,
decimal price) {...}
C12, Slide 50
50
25
4/16/2024
p2.Price = 61.50m;
isEqual = p1 == p2; // isEqual is false
C12, Slide 51
51
C12, Slide 52
52
26
4/16/2024
C12, Slide 53
53
C12, Slide 54
54
27
4/16/2024
C12, Slide 55
55
C12, Slide 56
56
28
4/16/2024
C12, Slide 57
57
The Save button event handler for the New Product form
private void btnSave_Click(object sender, EventArgs e)
{
if (IsValidData())
{
product = new(txtCode.Text, txtDescription.Text,
Convert.ToDecimal(txtPrice.Text));
this.Close();
}
}
C12, Slide 58
58
29
4/16/2024
C12, Slide 59
59
30