Jamie Jackson Jamie Jackson - 3 years ago 126
C# Question

Having trouble using methods and passing variables in c#

Im having trouble using methods in this assignment. my instructions are to

Use the variable that contains an instance of ScheduleStudentPresentationController(which is controller) to get the section. You will need to pass that method a studentId. Use the studentId variable defined above as the parameter.

The bottom line is how I thought I would call the method with studentId as a parameter and assign it to section, but its obviously not correct.

This is my 1st time programming in c#, so any direction, not matter how simple it seems would be great.

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);


Here is the definition for 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);
}
}
}

Answer Source

First off, your assignment explicitly says to initialize ScheduleStudentPresentationController and assign it to the class variable called controller. Therefore, you should use the this keyword to indicate that.

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

Now, regarding your issue, what you are doing here:

Section section = controller(studentId);

Is you are trying to "call" an object as if it were a method. This of course is not possible. Now, presumably, ScheduleStudentPresentationController has a method that you can use to get sections. Without showing us the code for that class I cannot show you exactly what you should do. However, it will be something like:

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

The above line of code calls the getSection method of the instance of the ScheduleStudentPresentationController class, that we have stored in the controller variable of our current class. The result that is returned by that method is stored in the new variable section.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download