Reputation: 17
I am trying to create a multi class program that creates student objects then allows you to change the value for the undeclared major for one of the student objects.
Here is my code:
StudentApp.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PA04CoddR
{
class StudentApp
{
public void Main()
{
DisplayTitle();
Student firstStudent = new Student("Robert", "Codd");
DisplayInfo(firstStudent);
Student secondStudent = new Student("Alexander", "Clemens", "FIN");
DisplayInfo(secondStudent);
GetMajor(firstStudent);
DisplayInfo(firstStudent);
TerminateProgram();
}
public void DisplayTitle()
{
Console.WriteLine("Programming Assignment 4 - Creating a Class - Robert S. Codd");
Console.WriteLine();
Console.WriteLine("____________________________________________________________");
}
public void DisplayInfo(Student newStudent)
{
Console.WriteLine("Frist Name: " + newStudent.GetFirstName);
Console.WriteLine("Last Name: " + newStudent.GetLastName);
Console.WriteLine("Major: " + newStudent.GetMajor);
}
public void GetMajor(Student newStudent)
{
Console.WriteLine("\tHello {0} {1}", newStudent.GetFirstName, newStudent.GetLastName);
Console.WriteLine("\tPlease enter your major: ");
string majorIn = Console.ReadLine();
Console.WriteLine();
newStudent.SetMajor(majorIn);
}
public void TerminateProgram()
{
Console.WriteLine("___________________________________________________________");
Console.WriteLine("PRESS ANY KEY TO TERMINATE...");
Console.Read();
}
}
}
Student.CS
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PA04CoddR
{
class Student
{
private string firstName;
private string lastName;
private string major;
public Student()
{
}
public Student(string firstName, string lastName)
{
firstName = GetFirstName;
lastName = GetLastName;
major = "Undeclared";
}
public Student(string firstName, string lastName, string major)
{
firstName = GetFirstName;
lastName = GetLastName;
major = GetMajor;
}
public string GetFirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string GetLastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public string GetMajor
{
get
{
return major;
}
set
{
major = value;
}
}
public string SetMajor(string majorIn)
{
major = majorIn;
return majorIn;
}
}
}
I have no errors being listed or given in the IDE but as soon as I try to compile the program it returns the error: "Does not contain a static 'main' method suitable for an entry point"
I did some research on here and other online resources and found a few things that seemed promising for solving my problem like changing the main method to static but as soon as I tried that every thing in my main method is returning a syntax error: "An object reference is required for the non-static field, method, or property"
Any help at all would be greatly appreciated, I am a learning programmer so I'm sure it is something fairly simple and I just do not full understand the concepts.
Upvotes: 1
Views: 424
Reputation: 7973
You must add static to public void Main like this:
public static void main(string[]args)
{
//Your code
}
If want use the other class you must add static to Student class and its all methods and to Student like StudentApp. This because a static method can call only other static methods.
Upvotes: 1
Reputation: 155005
Your Main
method must be static, change the other methods in StudentApp
to static too as they do not make use of any instance state.
Upvotes: 1
Reputation: 34846
You have this:
public void Main()
But you need to have this:
public static void Main()
Upvotes: 1
Reputation: 564333
Your main routine needs to be static:
public static void Main()
{
However, doing that will require you to create an instance of StudentApp
:
public static void Main()
{
var app = new StudentApp();
app.DisplayTitle(); // Call method on the instance
// Do the same for your other methods...
This is because your other methods that your Main()
methods use are instance methods, not static methods.
Upvotes: 4