如何在方法中调用方法并使用相同的变量

时间:2015-04-16 14:40:16

标签: c# console ref

我是编程新手,我遇到了C#问题。我想创建一个控制台程序,用户填写一些个人信息,控制台打印这些信息。我正在尝试使用ref,但我无法将用户的答案(来自方法GetStudentInfo)与print方法PrintStudentDetails连接起来。

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            GetStudentInfo();
            {
                string first = "";
                string last = "";
                string birthday = "";
                PrintStudentDetails(ref first, ref last, ref birthday);
            }
            GetTeacherInfo();
            GetCourseInfo();
            GetProgramInfo();
            GetDegreeInfo();
        }
        //student information
        static void GetStudentInfo()
        {
            Console.WriteLine("Enter the student's first name: ");
            string firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name: ");
            string lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthday: ");
            string birthDay = Console.ReadLine();
        }
        static void PrintStudentDetails(ref string first, ref string last, ref string birthday)
        {
            first = "test";
            last = "test";
            birthday = "test";
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你需要将字符串引用传递给GetStudentInfo,我认为最好使用而不是ref。
Out与ref相同,只是在方法返回之前out参数必须有一个值。

static void Main(string[] args)
{
    string first;
    string last;
    string birthday;
    GetStudentInfo(out first,out last,out birthday); 
    PrintStudentDetails (first, last, birthday);    
    GetTeacherInfo();
    GetCourseInfo();
    GetProgramInfo();
    GetDegreeInfo();
}
static void GetStudentInfo(out string firstName ,out string lastName,out string birthDay)
{
    Console.WriteLine("Enter the student's first name: ");
    firstName = Console.ReadLine();
    Console.WriteLine("Enter the student's last name: ");
    lastName = Console.ReadLine();
    Console.WriteLine("Enter the student's birthday: ");
    birthDay = Console.ReadLine(); 
}
static void PrintStudentDetails(string first, string last, string birthday)
{
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
    Console.ReadLine();
}

请参阅https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspxhttps://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx

答案 1 :(得分:0)

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //variables before the method that needs them, not after
            string first ="";
            string last ="";
            string birthday ="";
            GetStudentInfo(ref first, ref last, ref birthday);
            //removed extra brackets
            PrintStudentDetails(first, last, birthday);
            GetTeacherInfo();
            GetCourseInfo();
            GetProgramInfo();
            GetDegreeInfo();
        }
//student information
        //passed references in to this method
        static void GetStudentInfo(ref string firstName, ref string lastName, ref string birthday)
        {
            Console.WriteLine("Enter the student's first name: ");
            firstName = Console.ReadLine();
            Console.WriteLine("Enter the student's last name: ");
            lastName = Console.ReadLine();
            Console.WriteLine("Enter the student's birthday: ");
            birthday = Console.ReadLine(); 
        }
        static void PrintStudentDetails(string first, string last, string birthday)
        {
            //removed lines that reassigned variables
            Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday);
            Console.ReadLine();
        }
    }
}

你最大的问题是语法,它不能像写的那样工作。 Ref将变量地址传递给方法,基本上你需要在传递它之前分配变量,一旦它传递了方法中变量发生的任何事情,它也会发生在它之外,如果这是有道理的。

如果要在get info方法中调用print方法,请移动

 PrintStudentDetails(first, last, birthday);

里面

GetStudentInfo(ref string firstName, ref string lastName, ref string birthday)
{
    //move here and change the variables too firstName, lastName, birthday, instead of first, last, birthday    
}