2013-01-23 1 views
-2

Format1 클래스에 9 개의 문자열이 있습니다.이 클래스는 Format2 클래스에서 볼 수있는 다른 유형으로 변환하려고하지만 3 개의 문자열은 여전히 ​​문자열 유형으로 유지됩니다. 내가 만족 스러울 때까지 나는 그들과 놀기 시작했다.C# 메서드를 사용하여 객체 및 클래스에 액세스

내 Form1.cs 코드에서 볼 수 있듯이 버튼 클릭 이벤트에서 실제로하고 싶은 것은 getConvert() 메서드를 호출하여 모든 것을 처리하도록하는 것입니다. Apperantly 나는 뭔가를 놓치고있다. 나는 모든 것을 불러 내기 위해 나의 추한 6 줄을 사용해야한다.

코드에서 내 의견에 제 작동하지 않는 시도가 표시됩니다. 이번에 내가 뭘 잘못 했니? https://mega.co.nz/#!64QzERRR!Qit9SDZQ7kW7rNCAUUHHDRZUUvZY9z0ukgfuqVt00mE

public class Format1 
    { 
     public string Name { get; set; } 
     public string Year { get; set; } 
     public string Director { get; set; } 
     public string AverageRating { get; set; } 
     public string LeadingActor1 { get; set; } 
     public string LeadingActor2 { get; set; } 
     public string LeadingActor3 { get; set; } 
     public string Language { get; set; } 
     public string ImdbLink { get; set; } 


    } 



public class Format2 : Format1 
    { 
     public int Year { get; set; } 
     public int AverageRating {get; set;} 
     public string LeadingActors { get; set; } 
     public bool IsInEnglish { get; set; } 
     public bool HasImdbLink { get; set; } 


     public Format2 getConvert() 
     { 

      Format2 converted = new Format2(); 




     //converted.Name = textBox1.Text; 
     //textBox18.Text = converted.Name; 

      converted.Name = this.Name; 
      converted.Director = this.Director; 
      converted.ImdbLink = this.ImdbLink; 

      return converted; 
     } 
    } 





namespace as3_DVDproject 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     Format2 converted = new Format2(); 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void label8_Click(object sender, EventArgs e) 
     { 

     } 

     private void label9_Click(object sender, EventArgs e) 
     { 

     } 

     private void okButton_Click(object sender, EventArgs e) 
     { 
      //converted.getConvert(); 

      converted.Name = textBox1.Text; 
      textBox18.Text = converted.Name; 

      converted.Director = textBox3.Text; 
      textBox16.Text = converted.Director; 

      converted.ImdbLink = textBox9.Text; 
      textBox10.Text = converted.ImdbLink; 

     } 
    } 
} 

답변

1

더 많은 객체 지향 패턴이하는 것 중 하나 포맷 1 소요 Format2에 생성자를 추가하거나 Format2에게 포맷 1 소요 정적 메서드를 제공합니다 :

또한 여기 내 소스를 잡아 수 있습니다 Format2를 반환합니다. 실제 매핑 코드는 내게 너무 자세하게 보이지는 않지만, 그 장소 중 하나에 놓을 수 있습니다.

private void okButton_Click(object sender, EventArgs e) 
{ 
    Format1 one = new Format1(textBox1.Text, converted.Name, textBox3.Text, converted.Director); 
    Format2 two = new Format2(one); 
} 

개체에 양식으로 작성하는 대신 스스로 구성하는 방법을 알고 싶습니다.

+0

고맙습니다! 이것은 내가 찾고 있던 것이 었습니다. 나는 그것으로 실험하려고 노력할 것이다^_^ –

1

TextBox1이 TextBox1에 값을 할당하려고 시도했을 때 TextBox1이 TextBox1에 지정되었거나 그 반대의 경우 TextBox18이 해당 클래스에서 선언되지 않았기 때문에 실패합니다. Form1 클래스에 있고 Format2 클래스에서 액세스 할 수 없습니다.

+0

위대한 설명! 감사 –

관련 문제