【问题标题】:How to retrieve input from a function如何从函数中检索输入
【发布时间】:2013-07-29 00:20:30
【问题描述】:

我有下一个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Maman15cs
{
    public class ClassRoom
    {
        public string ClassNumber;
        public int NumberofPlaces;
        public int[,] DayandHour = new int[6,8];

        public void AddClassRoom()
        {
            Console.WriteLine("Enter the Class number, the Number of places\n");
            ClassNumber = Console.ReadLine().ToString();
            NumberofPlaces = int.Parse(Console.ReadLine());
            Console.WriteLine("Good, now enter the Day(1, 2, 3, 4, 5, 6) and after that you put the courses' number that are that day (In Order)");
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 8; j++)
                {

                    DayandHour[i,j] = int.Parse(Console.ReadLine());

                }

            }
        }

    }

    public class Course
    {
        public string CourseName;
        public int CourseNumber;
        public int StudentsNumber;
        public string TeacherName;
        public string ClassNumber;

        // Tuple<string, int, int, string, string>

        public void AddCourse(Course *course)
        {
            Console.WriteLine("Enter the Course's name, course's number, students number, teacher's name, and class' number\n");
            CourseName = Console.ReadLine().ToString();
            CourseNumber = int.Parse(Console.ReadLine());
            StudentsNumber = int.Parse(Console.ReadLine());
            TeacherName = Console.ReadLine().ToString();
            ClassNumber = Console.ReadLine().ToString();

        }
    }

    public class Program
    {

         void Main()
        {
            Course[] course = new Course[1000];
            ClassRoom[] classroom = new ClassRoom[1000];
            Course* coursePointer;


            int actionChoice;
            int courseCount = 0, classroomCount = 0;

             loop:

             Console.WriteLine("What do you want to do? (Enter number): \n  1) Add a new Course \n 2)Add a new class room \n 3)Add an existing class to an existing classroom \n 4)Read the information of a specific classroom \n 5)Read the information of all the classrooms \n 6)Read the information of a specific course \n 7)Delete a specific course \n 8)Update courses in the Time Table \n 9)Exit the program  \n");
             actionChoice = int.Parse(Console.ReadLine());

             switch (actionChoice)
             {

                 case 1: //Add a new Course

                    // course[classroomCount].AddCourse();

                   break;


             }

             goto loop;

        }
    }
}

我希望 AddCourse 函数返回或使用指针将输入添加到变量 course 中,我尝试了一些类似 list 的方法,但我对此没有那么经验。

【问题讨论】:

  • C# 中的类是通过引用传递的,因此您不需要显式使用指针。只需将实例化的类直接传递到您的函数中,它就可以满足您的需要。
  • 你能不能说的更具体一点,我没听懂你的回答。

标签: c# function pointers


【解决方案1】:

更改AddCourse 以创建一个新的Course 并返回它。

public Course AddCourse()
{
     var course = new Course();
     course.CourseName = Console.ReadLine().ToString();
     // ... more readlines
     return course;
 }

在主要:

List<Course> courses = new List<Course>();

case 1: courses.Add(AddCourse()); break;

【讨论】:

  • 感谢您的帮助。
【解决方案2】:

从 C 切换到 C# 后我遇到了类似的问题 :)

首先,您可以将Course[] course = new Course[1000]; 替换为var course = new List&lt;Course&gt;();List&lt;T&gt; 对于大多数场景来说要好得多 - 它没有确切的大小,您可以在任何位置“动态”添加任意数量的元素。

其次,所有类实例都作为引用传递。指针只能在一些罕见的场景中使用。

第三。 goto 也几乎从未在 C# 中使用过。语言中有大量的循环、枚举器等 - foreach、while、for

最后。在你的情况下,我会这样做:

public class Course
{
    public string CourseName;
    public int CourseNumber;
    public int StudentsNumber;
    public string TeacherName;
    public string ClassNumber;

    public static Course ReadCourse()
    {
        var rez = new Course();

        Console.WriteLine("Enter the Course's name, course's number, students number, teacher's name, and class' number\n");
        rez.CourseName = Console.ReadLine().ToString();
        rez.CourseNumber = int.Parse(Console.ReadLine());
        rez.StudentsNumber = int.Parse(Console.ReadLine());
        rez.TeacherName = Console.ReadLine().ToString();
        rez.ClassNumber = Console.ReadLine().ToString();

        return rez;
    }
}

public class Program
{   
    void Main()
    {
        var courses = new List<Course>();

        int actionChoice;
        while(1=1)
        {
            Console.WriteLine("What do you want to do? (Enter number): \n  1) Add a new Course \n 2)Add a new class room \n 3)Add an existing class to an existing classroom \n 4)Read the information of a specific classroom \n 5)Read the information of all the classrooms \n 6)Read the information of a specific course \n 7)Delete a specific course \n 8)Update courses in the Time Table \n 9)Exit the program  \n");
            actionChoice = int.Parse(Console.ReadLine());

            switch (actionChoice)
            {

                case 1: //Add a new Course
                    var new_course = Course.ReadCourse();
                    courses.Add(new_course);    
                    break;

                case 9: // Exit
                    return;
                default:
                    Console.WriteLine("Wrong input");
            }
        }
    }
}

这里有什么有趣的。静态方法 Course.ReadCourse 读取并返回 Course 的新实例。 defaultswitch 中的选择器。 return 退出应用程序。 List&lt;T&gt; 作为课程的存储。 new Course() 命令使用自动创建的隐式构造函数,因为没有定义任何构造函数。

【讨论】:

  • 我要感谢你们所有人,尤其是你们,你们知道我要做什么:)
【解决方案3】:

首先,设置一个列表来保存你所有的课程,不一定是一个数组(除非你真的需要一个数组):

List<Course> Courses = new List<Courses>();

更改您的 AddCourse 方法返回一个新实例化的 Course 对象:

Public Course AddCourse(){

Course newCourse = new Course();
<logic to populate the object>

return newCourse;

}

在您要添加课程的循环中,只需执行类似以下操作:

Courses.add(AddCourse());

然后您可以使用任何循环结构来遍历所有课程或 linq 以获得您需要的特定课程。

---编辑--

由于您坚持设置课程课程的方式(顺便说一句,这不是最佳实践),您需要将 AddCourse 方法更改为以下内容:

 public class Course
    {
        public string CourseName;
        public int CourseNumber;
        public int StudentsNumber;
        public string TeacherName;
        public string ClassNumber;
        public void AddCourse()
        {
            Console.WriteLine("Enter the Course's name, course's number, students number, teacher's name, and class' number\n");
            this.CourseName = Console.ReadLine().ToString();
            this.CourseNumber = int.Parse(Console.ReadLine());
            this.StudentsNumber = int.Parse(Console.ReadLine());
            this.TeacherName = Console.ReadLine().ToString();
            this.ClassNumber = Console.ReadLine().ToString();

        }
    }

那么你的循环方法中的调用需要是这样的:

Course NewCourse = new Course();
Courses.Add(NewCourse.AddCourse());

【讨论】:

  • 谢谢,我想我会试试这个。
  • 它告诉我 AddCourse 在当前上下文中不存在
  • 将 AddCourse 方法移动到同一个程序类中。要么,要么把它移到它自己的类中,然后先实例化那个类。
  • 您是如何在初始代码示例中调用该方法的?你会收到同样的错误。
  • 另外,您填充 Course 对象的逻辑不应该真正位于 Course 类本身中。它确实属于您的程序的主要输入逻辑,或处理它的单独类。
猜你喜欢
  • 2020-12-01
  • 2019-04-08
  • 2015-10-23
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多