변수 참조를 클래스로 전달한 다음 나중에 사용하고 싶습니다. 이 같은클래스 안팎으로 참조를 전달하는 방법
뭔가 :
// Create the comment Screen
string newCommentText = "";
commentsScreen = new CommentEntry(this, ref newCommentText);
commentScreen.ShowDialog();
...
_dataLayer.SaveOffComment(newCommentText);
그리고 주석 클래스 :
public partial class CommentEntry : Form
{
public CommentEntry(Control pControl, ref string commentResult)
{
InitializeComponent();
control = pControl;
// ***** Need a way for this to store the reference not the value. *****
_commentResult = commentResult;
}
private string _commentResult;
private void CommentEntry_Closing(object sender, CancelEventArgs e)
{
_commentResult = tbCommentText.Text.Trim();
}
}
는 newCommentText
닫는 방법에 _commentResult에서 설정 한 값을 가질 수 있음을 어떻게든지 있는가?
참고 : 분명히 내 수업에 변수를 설정하고 ShowDialog 다음에 액세스하는 것이 쉬울 것입니다. 이 예제는 내 실제 문제의 근사치이며 ShowDialog가 완료된 후 클래스의 모든 변수에 액세스하는 것은 불가능합니다.
을 더 간단한 옵션 또는 익명 대의원 (C# 2.0) (C#을 3.0) 대표와 람다 기능을 사용하는 것입니다 항상 클래스의 값을 랩핑합니다. –
참조가 아닌 포인터를 원하는 것처럼 들립니다. –
왜 불가능합니까? – Andy