2014-09-15 2 views
-2

내 코드는 다음과 같다 데이터 멤버와 같은 다른 클래스의 객체 ... 우리가 날짜 D에 액세스 할 수있는 방법을우리는

class Transaction 
{ 
    public class Date 
    { 
     public int day, month, year; 
    } 
    Date d; 
    double amount; 
    long acc_no; 
    string action; 
} 

을 선언 할 수 있습니다.

+2

같은 날짜를 저장 한 날짜 를 도시하지 않은 것입니까? 예, 할 수 있습니다. 중첩 클래스는 좋은 아이디어가있는 경우에만 수행해야합니다. 대부분의 경우이 작업을 원하지 않습니다. – BradleyDotNET

답변

1

는 우리가 어떻게 날짜 D 당신이 어떤 객체의 클래스 레벨 멤버에 액세스

같은 방법으로 액세스 할 수 있습니다. 클래스 Transaction의 인스턴스 내에서

, 당신은 클래스 레벨 멤버로 액세스합니다 :

this.d 

을에서 Transaction의 인스턴스 외부에 당신이 (당신도해야) d에 액세스 할 수 없습니다 그것 때문에 사적인 일원. (달리 명시하지 않는 한 C# 멤버는 기본적으로 비공개입니다.) Date의 인스턴스에서 Transaction 인스턴스의 컨텍스트에 있다고 보장 할 수 없으므로 해당 멤버에 직접 액세스 할 수 없습니다. (중첩 된 클래스는 그것이 해당 구조에서 항상 사용된다는 것을 보장하지 않습니다.) dDate의 인스턴스이므로 아무래도 문제가되지는 않으므로 Date에 있다면 액세스 만하면됩니다. 어쨌든.

1

댓글에서 언급 한 것처럼 일반적으로 좋은 방법은 아니지만 액세스 할 수 있습니다. 메서드 나 속성을 사용하지 않는 한 Transaction 클래스의 아무 곳에서나 액세스 할 수 있지만 외부는 아닙니다.

class Transaction 
{ 
    public class Date 
    { 
     public int day, month, year; 
    } 
    Date d; 
    double amount; 
    long acc_no; 
    string action; 


    public Date GetDate() 
    { 
     return d; // Access Date d by using a method 
    } 
} 
0

네임 스페이스 은행 {

공용 클래스 거래 { 공용 클래스 날짜 { 공공 INT의 년, 월, 일, } 공개 날짜 date = 새 날짜(); 공개 double 금액; public long acc_no; public string action; }

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<Transaction> transaction = new List<Transaction>(); 
     StreamReader sr = new StreamReader("transaction.csv"); 
     string data = sr.ReadLine(); 
     while (data != null) 
     { 
      string[] dataarray = data.Split(','); 
      string[] date_split = dataarray[0].Split('-'); 

      Transaction tran_obj = new Transaction(); 

      tran_obj.date.day = int.Parse(date_split[0]); 
      tran_obj.date.month = int.Parse(date_split[1]); 
      tran_obj.date.year = int.Parse(date_split[2]); 

      tran_obj.acc_no = long.Parse(dataarray[1]); 
      tran_obj.amount = double.Parse(dataarray[2]); 
      tran_obj.action = dataarray[3]; 

      transaction.Add(tran_obj); 
      data = sr.ReadLine(); 

     } 
     Console.WriteLine("Please enter the account number for which you are looking for"); 
     long new_acc_no = long.Parse(Console.ReadLine()); 
     foreach (Transaction t in transaction) 
     { 
      if (t.acc_no == new_acc_no) 
      { 
       Console.WriteLine(t.amount); 
       Console.WriteLine(t.date); 
       Console.WriteLine(t.action); 

      } 
     } 

     string s = Console.ReadLine(); 
    } 
    string s = Console.ReadLine(); 
} 

}

코드의 문제는 출력 콘솔에 난 가입일이 1994년 12월 2일