Reputation: 21
I'm new to c# (I started in January) and I need help with my code that I'm writing. On all the lines that come up with an error, it says
expected class, delegate, enum, interface, or struct
Here's my code:
using System;
namespace pawlowski_Catherine_Lab3
{
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
}
public Course ()
{
this.hours="3.00";
}
public Course(string description, string prefix, double number, double hours)
{
this.description=description;
this.prefix=prefix;
this.number=number;
this.hours=hours;
}
public override string ToString ()
{
return string.Format ("Course: "+prefix+"\nCourse Number: "+number+"\nDescription: "+description+"\nCredit hours: "+hours);
}
}
Pretty much, where the errors are, are on anything saying public at the beginning except the first one. Please help me figure out what specifically I'm doing wrong so I can fix it myself in the future.
Upvotes: 0
Views: 94
Reputation: 2067
Declare your contractor and method inside the class scope.
Replace your code with this
using System;
namespace pawlowski_Catherine_Lab3
{
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
public Course ()
{
this.hours="3.00";
}
public Course(string description, string prefix, double number, double hours)
{
this.description=description;
this.prefix=prefix;
this.number=number;
this.hours=hours;
}
public override string ToString ()
{
return string.Format ("Course: "+prefix+"\nCourse Number: "+number+"\nDescription: "+description+"\nCredit hours: "+hours);
}
}
}
Upvotes: 1
Reputation: 889
You should place all method and properties inside of the class declaration.
Something like :
using System;
namespace pawlowski_Catherine_Lab3
{
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
public Course ()
{
this.hours="3.00";
}
public Course(string description, string prefix, double number, double hours)
{
this.description=description;
this.prefix=prefix;
this.number=number;
this.hours=hours;
}
public override string ToString ()
{
return string.Format ("Course: "+prefix+"\nCourse Number: "+number+"\nDescription: "+description+"\nCredit hours: "+hours);
}
}
}
Upvotes: 2
Reputation: 2523
In the lines here:
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
} // Get rid of this line here
public Course ()
{
...
Upvotes: 1
Reputation: 30082
This will likely get closed, so:
Here's what you have (formatted):
using System;
namespace pawlowski_Catherine_Lab3 {
public class Course {
protected string description;
protected string prefix;
protected double number;
protected double hours;
}
public Course() {
this.hours = "3.00";
}
public Course(string description, string prefix, double number, double hours) {
this.description = description;
this.prefix = prefix;
this.number = number;
this.hours = hours;
}
public override string ToString() {
return string.Format("Course: " + prefix + "\nCourse Number: " + number + "\nDescription: " + description + "\nCredit hours: " + hours);
}
}
Here's what you should have (the constructor and methods are part of the class):
using System;
namespace pawlowski_Catherine_Lab3 {
public class Course {
protected string description;
protected string prefix;
protected double number;
protected double hours;
public Course() {
this.hours = "3.00";
}
public Course(string description, string prefix, double number, double hours) {
this.description = description;
this.prefix = prefix;
this.number = number;
this.hours = hours;
}
public override string ToString() {
return string.Format("Course: " + prefix + "\nCourse Number: " + number + "\nDescription: " + description + "\nCredit hours: " + hours);
}
}
}
Correct indenting is your friend here.
Upvotes: 2