2016-02-27 99 views
0

這是我在這裏的第一篇文章,所以大家好。我剛開始學習C#,並且在方法上沒有什麼問題。C# - 方法A的變量作爲方法B的參數

問題的關鍵 - 我必須創建程序,它首先向用戶詢問信息,然後通過另一種方法打印出來。 只有一段代碼:

namespace Uczymy3 
{ 
    class Student 
    { 
     public static void GetStudentInformation() 
     { 
      Console.WriteLine("Enter student's first name: "); 
      string firstName = Console.ReadLine(); 
      Console.WriteLine("Enter student's birth date: "); 
      DateTime birthDay = new DateTime(); 
      birthDay = DateTime.Parse(Console.ReadLine()); 
     } 
     public static void PrintStudentData(ref string firstName, ref DateTime birthDay) 
     { 
      Console.WriteLine("Student {0} was born in {1}", firstName, birthDay); 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Student.GetStudentInformation(); 
      XXXX; 
     } 
    } 
} 

我的問題在XXXX開始。我想要做的是這樣的: Student.PrintStudentData(Student.GetStudentInformation()。名字,...)

但我知道這是錯誤的。 那麼,如何將一個方法的變量作爲另一個方法的參數輸入呢?

或者,也許我應該把GetStudenInformation()的結果作爲變量放在類Student的作用域中,然後將它們用作PrintStudentData中的參數?

我希望一切都清楚解釋。

+0

創建的屬性,而不是局部變量的 –

回答

0

使用非靜態學生類並在類外部方法中定義字段。

class Student 
{ 

string firstName; 
DateTime birthDay; 


    public void GetStudentInformation() 
    { 
     Console.WriteLine("Enter student's first name: "); 
     firstName = Console.ReadLine(); 
     Console.WriteLine("Enter student's birth date: "); 

     birthDay = DateTime.Parse(Console.ReadLine()); 
    } 
    public void PrintStudentData() 
    { 
     Console.WriteLine("Student {0} was born in {1}", firstName, birthDay); 
    } 
} 
class Program 
{ 
    static void Main(string[] args) 
    { 
     var myStudent = new Student(); 
     myStudent.GetStudentInformation(); 
     myStudent.PrintStudentData(); 
    } 
} 
0

您最好創建一個Student Entity類並使第一個方法返回並將其傳遞給第二個方法。喜歡這個。

namespace Uczymy3 
{ 
    class Student 
    {  

    class StudentInfo{ 
     string firstName {get; set;} 
     DateTime birthDay {get;set;} 
    } 
     public static StudentInfo GetStudentInformation() 
     { 
      StudentInfo info = new StudentInfo(); 
      Console.WriteLine("Enter student's first name: "); 
      info.firstName = Console.ReadLine(); 
      Console.WriteLine("Enter student's birth date: "); 
      info.birthDay = DateTime.Parse(Console.ReadLine()); 
      return info; 
     } 
     public static void PrintStudentData(StudentInfo info) 
     { 
      Console.WriteLine("Student {0} was born in {1}", info.firstName, info.birthDay); 
     } 
    } 



    class Program 
    { 
     static void Main(string[] args) 
     { 
      var info = Student.GetStudentInformation(); 
      PrintStudentData(info); 
     } 
    } 

} 
0

難道這不能解決您的問題嗎?實例化類Student的對象。如果要打印數據,只需在對象上調用方法PrintStudentData即可。這是一個更加面向對象的方法。

PS:儘量避免不必要的公共靜態方法。谷歌的主題,它會給你很多新的見解。

namespace Uczymy3 
{ 
    class Student 
    { 
     private readonly string firstName; 
     private readonly DateTime birthDay; 

     public Student(string firstName, DateTime birthDay) 
     { 
      this.firstName = firstName; 
      this.birthDay = birthDay; 
     } 

     public void PrintStudentData() 
     { 
      Console.WriteLine("Student {0} was born in {1}", firstName, birthDay); 
     } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Enter student's first name: "); 
      string firstName = Console.ReadLine(); 
      Console.WriteLine("Enter student's birth date: "); 
      DateTime birthDay = DateTime.Parse(Console.ReadLine()); /* not really a user friendly way, but ok :) */ 

      Student student = new Student(firstName, birthDay); 
      student.PrintStudentData(); 
     } 
    } 
} 
+0

我設置爲「公開」以訪問這些方法,但感謝評論:d – Szkaplerny

+0

@Szkaplerny,公衆是沒有問題的,靜態的。 – bas

0

或者,也許我應該把結果GetStudenInformation的()作爲類學生範圍內的變量 ,然後用它們作爲 PrintStudentData參數呢?

這正是你應該做的。 您的代碼應該如下所示:此代碼

var studentInfo = Student.GetStudentInformation(); 
Student.PrintStudentData(studentInfo.Firstname, studentInfo.BirthDay); 

爲了有效,你將不得不從Student.GetStudentInformation返回學生的一個實例()。

既然你已經有一個學生類,添加properties它和創造條件,設置這些屬性構造:

class Student { 
    public string Firstname { get; private set; } 
    public DateTime BirthDay { get; private set; } 

    public Student(string firstname, DateTime birthDay) { 
     Firstname = firstname; 
     BirthDay = birthDay; 
    } 

    ... 
} 

現在你應該能夠從GetStudentInformation(返回學生的實例):

public static Student GetStudentInformation() 
{ 
      ... 
      return new Student(firstName, birthDay); 
} 

順便說一下,在這種情況下PrintStudentData的參數不應該是「ref」。 「ref」表示您傳遞參數by reference而不是值。

+0

那麼我應該使用'out'選項嗎?在某處我讀到'ref'和'out'之間的區別之一就是'out'變量必須在方法體內初始化。但在我的例子中,變量沒有在Print的主體中初始化,所以我選擇了'ref'。 我該怎麼辦? – Szkaplerny

+0

@Szkaplerny「out」表示您通過引用傳遞變量,但您也允許函數更改引用本身(https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx)。除非是基元(String,Integer,Boolean等),否則C#中所有類的實例都會自動按引用傳遞。但在你的情況下,不需要通過引用傳遞firstName和birthDay,因爲你只是輸出它們。 – areller

相關問題