在C#中使用方法和传递变量遇到麻烦



im在此分配中使用方法遇到麻烦。我的说明是

使用包含ScheduleStudentPresentationController(Controler)的实例的变量来获取该部分。您将需要通过该方法作为学生ID。使用上面定义为参数的StudentId变量。

最重要的是,我认为我将使用StudentId的方法称为参数并将其分配给部分,但显然不正确。

这是我在C#中的第一次编程,所以任何方向,无论它看起来都很简单。

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace A4_Skeleton
{
    public partial class FormStudentSignUpForPresentationSlot : Form
    {
        private ScheduleStudentPresentationController controller;
        private Section selectedSection;
        public FormStudentSignUpForPresentationSlot()
        {
            InitializeComponent();

    //***********************************************************************
        // 1.
        //Instantiate an instance of the ScheduleStudentPresentationController
        // and assign it to the class variable named "controller"
        //***********************************************************************
        controller = new ScheduleStudentPresentationController();

        }
        private void buttonGetSection_Click(object sender, EventArgs e)
        {
            // Clear out any residual info
            labelSectionInfo.Text = "";
            int studentId = 1;

   //***********************************************************************
        // 2.
        // Use the variable that contains an instance of 
        //ScheduleStudentPresentationController
        // to get the section.  You will need to pass that method a 
        //studentId.
        // Use the studentId variable defined above as the parameter
        //***********************************************************************
    Section section = controller(studentId);

这是ScheduleStudentPresentationController的定义

using System;
using System.Collections.Generic;
namespace A4_Skeleton
{
    public class ScheduleStudentPresentationController
    {
        private PresentationSchedule presentationSchedule;
        public ScheduleStudentPresentationController()
        {
            presentationSchedule = new PresentationSchedule();
        }
        public Section getSection(int studentId)
        {
            SectionEnrolledStudent sectionEnrolledStudent = new 
            SectionEnrolledStudent();
            return sectionEnrolledStudent.getSection(studentId);
        }
        public Dictionary<DateTime, List<Slot>> getAvailableSlots(int 
        sectionId)
        {
            return presentationSchedule.getAvailableSlots(sectionId);
        }
        public bool selectSlot(int sectionId, int studentId, DateTime 
        slotDate, int slotNum)
        {
            return presentationSchedule.selectSlot(sectionId, studentId, 
            slotDate, slotNum);
        }
    }
}

首先,您的分配明确表示要初始化 ScheduleStudentPresentationController并将其分配给 class 变量称为控制器。因此,您应该使用this关键字来表示。

public FormStudentSignUpForPresentationSlot()
{
    InitializeComponent();
    this.controller = new ScheduleStudentPresentationController();
}

现在,关于您的问题,您在这里做什么:

Section section = controller(studentId);

您是否正在尝试"调用"一个对象,就好像是一种方法一样。当然,这是不可能的。现在,大概ScheduleStudentPresentationController有一种可以用来获取部分的方法。没有向我们展示该类的代码,我无法完全向您展示您应该做的事情。但是,这将是:

Section section = this.controller.getSection(studentId);

上面的代码行调用了我们已存储在当前类的controller变量中的ScheduleStudentPresentationController类实例的getSection方法。该方法返回的结果存储在新的变量section中。

最新更新