2015-07-05 46 views
1

我想通過PrintStudentDetails(字符串優先,字符串最後,字符串生日)方法從GetStudentInformation()方法打印值。請查看下面的代碼:如何在C#方法中打印多個值?

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

namespace Module3_HomeWork 
{ 
    class Program 
    { 
     static void GetStudentInformation() 
     { 
      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 birthdate: "); 
      DateTime birthdate = Convert.ToDateTime(Console.ReadLine()); 
      Console.WriteLine("Enter the student's address line 1: "); 
      string add1 = Console.ReadLine(); 
      Console.WriteLine("Enter the student's address line 2: "); 
      string add2 = Console.ReadLine(); 
      Console.WriteLine("Enter the student's city: "); 
      string city = Console.ReadLine(); 
      Console.WriteLine("Enter the student's state: "); 
      string state = Console.ReadLine(); 
      Console.WriteLine("Enter the student's zip code: "); 
      int zip = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Enter the student's country: "); 
      string country = Console.ReadLine(); 
     } 
     static void PrintStudentDetails(string first, string last, string birthday) 
     { 
      Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday); 
     } 
     static void Main(string[] args) 
     { 
      GetStudentInformation(); 
      PrintStudentDetails(first, last, birthday); 
     } 

     public static string first { get; set; } 

     public static string last { get; set; } 

     public static string birthday { get; set; } 
    } 
} 

如何使用只有方法而不是類打印值?我研究了它,發現了大約outref,任何幫助或指導將不勝感激。

問候

+0

使用屬性而不是局部變量 –

回答

2

如果你想要的是輸入姓氏,名字和出生日期,然後輸出它們,你可以改變以下三個行:

string firstName = Console.ReadLine(); 
string lastName = Console.ReadLine(); 
DateTime birthdate = Convert.ToDateTime(Console.ReadLine()); 

爲以下:

first = Console.ReadLine(); 
last = Console.ReadLine(); 
birthday = Convert.ToDateTime(Console.ReadLine()); 

然後你的程序應該按照你的預期運行。

但是既然你提到outref,也許你真正想要的是以下幾點:

​​
1

我不太清楚是什麼?你的意思是打印值僅方法不類。但我想下面的例子可以幫助你。

void PrintDetails(ref string s1, ref string s2, ref string s3) 
{ 
    s1 = "Some text goes here"; 
    s2 = myNumber.ToString(); 
    s3 = "some more text"; 
} 

現在在調用這個函數只是使用下面的語法:

public void SomeOtherFunction() 
{ 
    string sMyValue1 = null; 
    string sMyValue2 = null; 
    string sMyValue3 = null; 

    PrintDetails(sMyValue1, sMyValue2, sMyValue3); 
    //Now you have all the values inside your local variable. 
} 
0

使用outref關鍵字會幫助你從方法返回多個值,如果你真的想要做的是方式,但使用屬性可能是一個更好的解決方案。不管怎麼說,這是它會是什麼樣子使用out關鍵字:

static void GetStudentInformation(out string firstName, out string lastName, out DateTime birthDate, 
    out string add1, out string add2, out string city, out string state, out int zip, out string country) 
{ 
    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 birthdate: "); 
    birthDate = Convert.ToDateTime(Console.ReadLine()); 
    Console.WriteLine("Enter the student's address line 1: "); 
    add1 = Console.ReadLine(); 
    Console.WriteLine("Enter the student's address line 2: "); 
    add2 = Console.ReadLine(); 
    Console.WriteLine("Enter the student's city: "); 
    city = Console.ReadLine(); 
    Console.WriteLine("Enter the student's state: "); 
    state = Console.ReadLine(); 
    Console.WriteLine("Enter the student's zip code: "); 
    zip = Convert.ToInt32(Console.ReadLine()); 
    Console.WriteLine("Enter the student's country: "); 
    country = Console.ReadLine(); 
} 
static void PrintStudentDetails(string first, string last, string birthday) 
{ 
    Console.WriteLine("{0} {1} was born on: {2}", first, last, birthday); 
} 

static void Main(string[] args) 
{ 
    string firstName, lastName, add1, add2, city, state, country; 
    DateTime birthDate; 
    int zip; 

    GetStudentInformation(out firstName, out lastName, out birthDate, out add1, out add2, out city, out state, out zip, out country); 
    PrintStudentDetails(firstName, lastName, birthDate.ToShortDateString()); 
} 

你也可以用ref關鍵字替換所有出關鍵字,你會得到相同的效果。你只需要在通過引用GetStudentInformation方法傳遞它們之前將變量初始化爲某個值。