所以基本上我的问题是我的程序中有一个方法可以为学生设置结构中的数据,如下所示:
public static void addingstudent(){
student student;
AddStudent details = new AddStudent();
student.name = details.setName();
student.course = details.setCourse();
student.studentno = details.setStudentNumber();
student.year = details.setYear();
menu();
}
AddStudent类包含一些方法,用于要求用户输入Name等并返回它们,这个类中的一个例子是:
public static int setStudentNo(){
Console.Write("Please enter Student Number: ");
int StudentNo = int.Parse(Console.ReadLine());
return StudentNo;
}
然后我尝试访问该数据并将其显示在屏幕上:
public static void getstudent(){
student student;
student.displayDetails();
}
这只是显示所有变量的空值,但是如果我在首先设置它正确显示的值后调用它,最后结构如下:
public struct student{
public String name;
public int studentno;
public String course;
public int year;
public void displayDetails(){
Console.WriteLine("Name: " + name);
Console.WriteLine("Student Number: "+studentno);
Console.WriteLine("Course: "+course);
Console.WriteLine("Year: "+year);
}
答案 0 :(得分:3)
您student student
中的用户addingstudent()
,student student
中的另一个getstudent()
,据我所知......您没有使用同一个对象...
你可以拥有一个具有结构和两个函数的类
class A
{
studnet studnet;
public static void addingstudent()
{
AddStudent details = new AddStudent();
student.name = details.setName();
student.course = details.setCourse();
student.studentno = details.setStudentNumber();
student.year = details.setYear();
menu();
}
public static void getstudent()
{
student.displayDetails();
}
}
然后只需创建一个新的A类并使用您想要的数据进行操作
答案 1 :(得分:0)
首先创建可变结构是一个坏主意,如果数据是可变的,你真的应该创建一个类。
另外,您没有将stuct作为参数传递,例如在getstudent
中每次创建一个新的学生对象,这就是student.displayDetails();
显示为null的原因,因为您还没有设置在你打电话之前,你在线上创建的学生的任何属性。
public static void getstudent()
{
Student student; // creates a new instance of the struct...
student.displayDetails(); // obviously student properties are null...
}
你真的应该做以下事情。
public class Student
{
public Student()
{
this.SetName();
this.SetNumber();
this.SetCourse()
this.SetYear();
}
public override string ToString()
{
return string.Format(
"Name: {0}{4}Number: {1}{4}Course: {2}{4}Year: {3}{4}",
this.Name,
this.Number,
this.Course,
this.Year,
Environment.NewLine);
}
public void DisplayDetails()
{
Console.WriteLine(this.ToString());
}
public string Name { get; private set; }
public string Number{ get; private set; }
public string Course { get; private set; }
public string Year { get; private set; }
public static void SetName()
{
Console.WriteLine("Please enter Name: ");
this.Name = Console.ReadLine();
}
public static void SetNumber()
{
Console.WriteLine("Please enter Number: ");
this.Number = int.Parse(Console.ReadLine());
}
public static void SetCourse()
{
Console.WriteLine("Please enter Course: ");
this.Course = Console.ReadLine();
}
public static void SetYear()
{
Console.WriteLine("Please enter Year: ");
this.Year = int.Parse(Console.ReadLine());
}
}
public class App
{
public static void Main()
{
Student student1 = new Student(); // Create a student
student1.DisplayDetails(); // show the details
student1.SetName(); // change the name
student1.SetYear(); // change the year
student1.DisplayDetails(); // show the new details
// etc...
}
}