Create a class that might provides a property for an instance variable based on
ID: 3763760 • Letter: C
Question
Create a class that might provides a property for an instance variable based on the example in this question.
Here is the example:
// Fig. 4.7: GradeBook.cs
2 // GradeBook class that contains a courseName instance variable,
3 // and a property to get and set its value.
4 using System;
5
6 public class GradeBook
7 {
8 private string courseName; // course name for this GradeBook
9
10 // property to get and set the course name
11 public string CourseName
12 {
13 get
14 {
15 return courseName;
16 } // end get
17 set
18 {
19 courseName = value;
20 } // end set
21 } // end property CourseName
22
23 // display a welcome message to the GradeBook user
24 public void DisplayMessage()
25 {
26 // use property CourseName to get the
27 // name of the course that this GradeBook represents
28 Console.WriteLine( "Welcome to the grade book for {0}!",
29 ); // display property CourseName
30 } // end method DisplayMessage
31 } // end class GradeBook
Explanation / Answer
Below is the main class where how to use the above class is given:
class Program
{
static void Main(string[] args)
{
GradeBook Book1 = new GradeBook();
Book1.CourseName="A Couse Name";
Book1.DisplayMessage();
Console.ReadKey(true);
}
}
There is a small error in you given code at line 28 and 29, you missed to include the variable - it should be as follows:
Console.WriteLine( "Welcome to the grade book for {0}!",courseName ); // display property CourseName
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.