I have Bolded Where the errors occur. Error 1 Only assignment, call, increment,
ID: 3555112 • Letter: I
Question
I have Bolded Where the errors occur.
Error 1 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Error 2 The event 'Student.NewCredits' can only appear on the left hand side of += or -= (except when used from within the type 'Student')
Error 3 No overload for 'NewCredits' matches delegate 'System.EventHandler'
// This file contains three classes
// Student class contains Student information
// EventListener contains method that executes
// when Student completes a course so that Student
// knows how many credit hours remain until graduation
// Demo class demonstrates the other two classes
using System;
class Student
{
public event EventHandler NewCredits;
public int IdNum {get; set;}
public string LastName {get; set;}
public string FirstName {get; set;}
public int CreditsCompleted {get; set;}
public void AddCreditsCompleted(int newCredits)
{
CreditsCompleted += newCredits;
OnNewCredits(EventArgs.Empty);
}
public void OnNewCredits(EventArgs e)
{
NewCredits(this, e);
}
}
class EventListener
{
private Student stu;
const int CREDITS_TO_GRADUATE = 120;
const int SOPHOMORE = 30;
const int JUNIOR = 60;
const int SENIOR = 90;
public EventListener(Student student)
{
stu.NewCredits == new EventHandler(NewCredits);
}
private void NewCredits(EventArgs e)
{
Console.WriteLine("{0} {1} has completed new credits:", stu.FirstName, stu.LastName);
if(stu.CreditsCompleted >= CREDITS_TO_GRADUATE)
Console.WriteLine("Credits = {0}. Qualified for graduation.",
stu.CreditsCompleted);
else
{
Console.Write(" {0} credits completed; {1} credits to go.",
stu.CreditsCompleted, CREDITS_TO_GRADUATE - stu.CreditsCompleted);
Console.Write(" Current status is ");
if(stu.CreditsCompleted == SENIOR)
Console.WriteLine("senior.");
else if(stu.CreditsCompleted == JUNIOR)
Console.WriteLine("junior.");
else if(stu.CreditsCompleted == SOPHOMORE)
Console.WriteLine("sophomore.");
else Console.WriteLine("freshman.");
}
}
}
class DebugThirteen1
{
static void Main()
{
Student Student();
EventListener listener = new EventListener(oneStu);
oneStu.IdNum = 2345;
oneStu.LastName = "Flood";
oneStu.FirstName = "Megan";
oneStu.AddCreditsCompleted(15);
oneStu.AddCreditsCompleted(15);
oneStu.AddCreditsCompleted(14);
oneStu.AddCreditsCompleted(14);
oneStu.AddCreditsCompleted(17);
oneStu.AddCreditsCompleted(16);
oneStu.AddCreditsCompleted(15);
oneStu.AddCreditsCompleted(18);
}
}
Explanation / Answer
try this........
public EventListener(Student student)
with this one
public EventListener(Student stu)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.