如何从方法返回信息

时间:2015-04-18 12:01:30

标签: c#

我想从两种方法(学生和老师)获取信息并将信息发送到另一种方法PrintStudentDetails - 打印到控制台 - 但是如何将字符串传递给打印方法?我试过把字符串传回去,但我无法让它工作?

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

namespace Assignment3
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInfo();

        //    PrintStudentDetails();
        }   

        static void GetStudentInfo()
        {
            // Get student details
          Console.WriteLine("Enter the student's first name: ");
          string sfirst = Console.ReadLine();
          Console.WriteLine("Enter the student's last name");
          string lastName = Console.ReadLine();
          Console.WriteLine("Enter the students birthday");
          string studentsBirthday = Console.ReadLine();

        }

        static void GetTeacherInfo()
        {
            // Code to get teacher name, course, program and degree
            Console.WriteLine("Enter the teachers first name: ");
            string teacherfirstName = Console.ReadLine();
            Console.WriteLine("Enter name of course: ");
            string teacherlastName = Console.ReadLine();
            Console.WriteLine("Enter program: ");
            string programName = Console.ReadLine();
            Console.WriteLine("Enter Degree: ");
            string degreeName = Console.ReadLine();            
        }

        static void PrintStudentDetails(string first, string last, string birthday)
        {
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }            
    }
}

3 个答案:

答案 0 :(得分:2)

执行此操作的典型方法是使用return语句从函数返回收集的数据。由于您要收集多个数据项,因此需要以某种方式将它们捆绑起来。

处理此问题的一种方法是使用具有每个数据项的属性的class,然后从该函数返回该类的实例。 (我将在下面显示学生信息的示例;教师信息是相似的。)像这样:

class StudentInfo
{
    public string firstName;
    public string lastName;
    public string birthday;
}

您的函数GetStudentInfo()可以创建并返回类StudentInfo的实例:

static StudentInfo GetStudentInfo()
{
  // Get student details
  StudentInfo studentInfo = new StudentInfo();

  Console.WriteLine("Enter the student's first name: ");
  studentInfo.firstName = Console.ReadLine();
  Console.WriteLine("Enter the student's last name");
  studentInfo.lastName = Console.ReadLine();
  Console.WriteLine("Enter the students birthday");
  studentInfo.birthday = Console.ReadLine();

  return studentInfo;
}

您的打印功能可以保持原样,要求调用者从StudentInfo实例中提取属性:

static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    Console.ReadLine();
}

这样称呼:

static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo.firstName, studentInfo.lastName, studentInfo.birthday);
}

或者,您可以将StudentInfo直接传递给打印功能:

static void PrintStudentDetails(StudentInfo info)
{
    Console.WriteLine("{0} {1} was born on: {2}", info.firstName, info.lastName, info.birthday);
    Console.ReadLine();
}

并将其称为:

static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    PrintStudentDetails(studentInfo);
}

或更简单,无需创建中间studentInfo变量:

static void Main(string[] args)
{
    PrintStudentDetails(GetStudentInfo());
}

请注意,由于函数重载,您实际上可以使用PrintStudentDetails()的两个版本。 C#将根据参数类型调用正确的函数:

static void Main(string[] args)
{
    StudentInfo studentInfo = GetStudentInfo();
    // using separate arguments...
    PrintStudentDetails(studentInfo.firstName, studentInfo.lastName, studentInfo.birthday);

    // or passing a StudentInfo instance...
    PrintStudentDetails(GetStudentInfo());
}

说了这么多,还有其他数据结构可用于实现与类相同的效果,例如:键/值映射,列表,元组或类似。

答案 1 :(得分:0)

您必须使用return类型作为string或您希望从函数返回值的任何其他数据类型。

目前您的功能属于void类型。意思是,他们没有回报。如果要从函数中获取字符串值,请将其声明为string类型并返回字符串值。

类似的东西:

static string GetTeacherInfo()
        {
            // Code to get teacher name, course, program and degree
            Console.WriteLine("Enter the teachers first name: ");
            string teacherfirstName = Console.ReadLine();
            Console.WriteLine("Enter name of course: ");
            string teacherlastName = Console.ReadLine();
            Console.WriteLine("Enter program: ");
            string programName = Console.ReadLine();
            Console.WriteLine("Enter Degree: ");
            string degreeName = Console.ReadLine();            
            return degreeName;
        }

希望这有帮助。

答案 2 :(得分:0)

您好我猜您也在关注EDX DEV2014x MOOC; - )

即使对于非初学者,说明也不容易理解。 只需调用您的方法

PrintStudentDetails(string first, string last, string birthday)

来自方法

static void GetStudentInfo()

祝你好运完成任务