You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Instructions/Labs/l2p2-lp1-m1-exercise-create-classes-and-objects-in-csharp.md
+19-14Lines changed: 19 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -183,6 +183,8 @@ Use the following steps to complete this task:
183
183
184
184
## Update the Program.cs file to create instances of the BankCustomer class
185
185
186
+
The `new` operator is used to create objects based on a class constructor. Each object has its own data fields, and the values assigned to the fields can be different for each object.
187
+
186
188
In this task, you update the Program.cs file to create instances of the `BankCustomer` class and run the app to verify that the instances are created successfully.
187
189
188
190
Use the following steps to complete this task:
@@ -294,6 +296,8 @@ Use the following steps to complete this task:
294
296
295
297
## Add public fields and updated constructors to the BankCustomer class
296
298
299
+
Public fields are accessible from outside the class. They can be read from and written to by any code that has access to the object. Public fields are often used to expose object data to other classes.
300
+
297
301
In this task, you add public fields to the `BankCustomer` class, update the instance constructors to use additional parameters, and then update the Program.cs file to access each object's public data.
298
302
299
303
Use the following steps to complete this task:
@@ -359,11 +363,11 @@ Use the following steps to complete this task:
359
363
360
364
```
361
365
362
-
The constructor initializes the `fName`, `lName`, and `accountId` fields with the values of the `firstName`, `lastName`, and `customerIdNumber` parameters, respectively.
366
+
The constructor initializes the `fName`, `lName`, and `customerId` fields with the values of the `firstName`, `lastName`, and `customerIdNumber` parameters, respectively.
363
367
364
368
1. Switch to the Program.cs file
365
369
366
-
1. To create local variables for `firstName`, `lastName`, and `accountNumber` just below the `namespace` declaration, enter the following code:
370
+
1. To create local variables for `firstName`, `lastName`, and `customerIdNumber` just below the `namespace` declaration, enter the following code:
367
371
368
372
```csharp
369
373
@@ -430,7 +434,6 @@ Use the following steps to complete this task:
@@ -443,7 +446,7 @@ Use the following steps to complete this task:
443
446
444
447
public class BankCustomer
445
448
{
446
-
// add public fields for fName, lName, and accountID
449
+
// add public fields for fName, lName, and customerId
447
450
public string fName = "Tim";
448
451
public string lName = "Shao";
449
452
public string customerId = "1010101010";
@@ -480,10 +483,12 @@ Use the following steps to complete this task:
480
483
481
484
```
482
485
483
-
Notice that customer1 and customer2 share the same ID number. The public fields are initialized with default values. Although the constructors update the fields with the values passed as parameters, there are clearly some issues to address.
486
+
Notice that the first two customers share the same ID number. Public fields in the BankCustomer class are initialized using default values. Although the constructors update some fields with the values passed as parameters, there are issues that must be addressed.
484
487
485
488
## Update the BankCustomer class using static members to ensure unique customer IDs
486
489
490
+
Static fields are initialized before an instance of the class is created. They're accessed using the class name, not an instance of the class, and they're shared among all instances of the class. Static constructors are called when a class is loaded into memory. A static constructor is called only once, regardless of how many instances of the class are created.
491
+
487
492
In this task, you update the `BankCustomer` class using a static field and static constructor to ensure unique `customerId` values are assigned to each new customer object.
488
493
489
494
Use the following steps to complete this task:
@@ -496,7 +501,7 @@ Use the following steps to complete this task:
496
501
497
502
public class BankCustomer
498
503
{
499
-
// add public fields for fName, lName, and accountID
504
+
// add public fields for fName, lName, and customerId
500
505
public string fName = "Tim";
501
506
public string lName = "Shao";
502
507
public string customerId = "1010101010";
@@ -555,9 +560,9 @@ Use the following steps to complete this task:
555
560
556
561
```
557
562
558
-
The static constructor is called when the `BankCustomer` class is loaded into memory. The static constructor initializes the `nextCustomerId` field with a random value between 10,000,000 and 20,000,000.
563
+
The static constructor is called when the `BankCustomer` class is loaded into memory, and before any instances of the class are created. The static constructor initializes the `nextCustomerId` field with a random eight digit integer.
559
564
560
-
1. To update the parameterless constructor to assign the `customerId` field using the `nextCustomerId` field, add the following code to the `BankCustomer` class definition:
565
+
1. To assign a unique value to `customerId` inside your parameterless constructor, update the constructor to match the following code:
561
566
562
567
```csharp
563
568
@@ -568,12 +573,12 @@ Use the following steps to complete this task:
568
573
569
574
```
570
575
571
-
The constructor initializes the `customerId` field using the `nextCustomerId` field. Notice that `nextCustomerId` is incremented by 1 before the `customerId` field is assigned a value.
576
+
The updated constructor initializes the `customerId` field using the already initialized `nextCustomerId` field. Notice that `nextCustomerId` is incremented by 1 before the `customerId` field is assigned a value.
572
577
573
578
> [!NOTE]
574
-
> The `this` keyword is used to refer to the current instance of the class. It's used to access fields, properties, and methods of the current instance.
579
+
> The `this` keyword is used to refer to the current instance of the class. It's used to access fields, properties, and methods of the current instance. The `this` keyword is not available in a static constructor.
575
580
576
-
1. To update the constructor that accepts the first and last name fields, add the following code to the `BankCustomer` class definition:
581
+
1. To assign a unique value to `customerId` inside your constructor that accepts `firstName` and `lastName` parameters, update the constructor to match the following code:
577
582
578
583
```csharp
579
584
@@ -586,11 +591,11 @@ Use the following steps to complete this task:
586
591
587
592
```
588
593
589
-
Once again, the constructor initializes the `customerId` field using the incremented `nextCustomerId` field value.
594
+
Once again, the constructor initializes the `customerId` field using an incremented `nextCustomerId` value.
590
595
591
596
1. Delete the constructor accepts the `firstName`, `lastName`, and `customerIdNumber` parameters.
592
597
593
-
The static constructor ensures that each new instance of the `BankCustomer` class is assigned a unique customer ID. You already have a constructor that accepts the `firstName` and `lastName` parameters, so a constructor that accepts all three parameters is no longer needed.
598
+
Ensuring unique customer IDs includes removing the opportunity to create a new customer with an externally provided ID. Also, your parameterless and static constructors ensure that every new instance of the `BankCustomer` class is assigned a unique customer ID.
594
599
595
600
1. Switch to the Program.cs file.
596
601
@@ -681,7 +686,7 @@ Use the following steps to complete this task:
681
686
682
687
In this task, you create an `BankAccount` class that initializes a combination of public and static fields. You also create constructors that initialize the fields with default values and values passed as parameters.
683
688
684
-
You need a `BankAccount` class that includes the following fields:
689
+
You want a `BankAccount` class that includes the following fields:
685
690
686
691
- `accountNumber` - A public field that stores the account number.
687
692
- `balance` - A public field that stores the account balance.
0 commit comments