-1

I am new in programming and I am stuck in a C# problem. I want to create a console program where the user fills in some personal information and the console prints these information. I am trying to use ref but I can't connect the answers from the users (from method GetStudentInfo) with the print method 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();
        }
    }
}
David L
  • 32,885
  • 8
  • 62
  • 93
fold yourdesign
  • 35
  • 1
  • 1
  • 2

2 Answers2

1

You need to pass the string refs to GetStudentInfo and i think that is better to use out instead of ref.
Out is the same as ref with the addition that out parameters must have a value before the method returns.

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

see https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx and https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx

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

Your biggest problem is syntax, it just could not work as written. Ref passes the variables address to the method, basically you need to have the variable assigned before you can pass it, once it's passed anything that happens to the variable inside the method will happen to it outside of it as well, if that makes sense.

If you want to call the print method in your get info method move

 PrintStudentDetails(first, last, birthday);

inside

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    
}
Scriven
  • 438
  • 4
  • 12