C# 如何将引用传入和传出类

C# 如何将引用传入和传出类,c#,class,reference,C#,Class,Reference,我想把一个变量的引用传递到一个类中,使用它,然后稍后再把它取出来 大概是这样的: // Create the comment Screen string newCommentText = ""; commentsScreen = new CommentEntry(this, ref newCommentText); commentScreen.ShowDialog(); ... _dataLayer.SaveOffComment(newCommentText); public inter

我想把一个变量的引用传递到一个类中,使用它,然后稍后再把它取出来

大概是这样的:

// Create the comment Screen
string newCommentText = "";
commentsScreen = new CommentEntry(this, ref newCommentText);

commentScreen.ShowDialog();

...

_dataLayer.SaveOffComment(newCommentText);
public interface ICommented
{
    string Comment { get; set; }
}

public class MyClass : ICommented
{
    public string Comment { get; set; }
}

public partial class CommentEntry : Form
{
    public CommentEntry(Control pControl, ICommented commented)
    {
        InitializeComponent();
        control = pControl;

        // ***** Need a way for this to store the reference not the value. *****
        _commented = commented;
    }


    private ICommented _commented;

    private void CommentEntry_Closing(object sender, CancelEventArgs e)
    {
        _commented.Comment = tbCommentText.Text.Trim();
    }
}
public partial class CommentEntry : Form { 
  public CommentEntry(Control pControl, Action<string> reportResult) { 
    InitializeComponent(); 
    control = pControl; 
    // Store the delegate in a local field (no problem here)
    _reportResult = reportResult;    
  } 

  private Action<string> _reportResult; 

  private void CommentEntry_Closing(object sender, CancelEventArgs e) { 
    // Invoke the delegate to notify the caller about the value
    _reportResult(tbCommentText.Text.Trim()); 
  } 
} 
然后在comment类中:

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完成后无法访问类中的任何变量。

将CommentEntry类的newComment属性设置为新属性。

这永远不会对字符串起作用,因为字符串是不可变的,变量将更改为指向新实例


你有两个基本的选择。第一种方法是简单地为结果提供一个getter,以便以后需要时可以访问它。另一个选项是让所有者传入一个委托方法,该委托方法可以通过传入结果值来调用。当CommentEntry关闭时,所有者将收到该值。

在这里,试试这家伙在做什么。

通常不能在C#中直接存储“对引用的引用”,但可以执行以下操作:

// Create the comment Screen
string newCommentText = "";
commentsScreen = new CommentEntry(this, ref newCommentText);

commentScreen.ShowDialog();

...

_dataLayer.SaveOffComment(newCommentText);
public interface ICommented
{
    string Comment { get; set; }
}

public class MyClass : ICommented
{
    public string Comment { get; set; }
}

public partial class CommentEntry : Form
{
    public CommentEntry(Control pControl, ICommented commented)
    {
        InitializeComponent();
        control = pControl;

        // ***** Need a way for this to store the reference not the value. *****
        _commented = commented;
    }


    private ICommented _commented;

    private void CommentEntry_Closing(object sender, CancelEventArgs e)
    {
        _commented.Comment = tbCommentText.Text.Trim();
    }
}
public partial class CommentEntry : Form { 
  public CommentEntry(Control pControl, Action<string> reportResult) { 
    InitializeComponent(); 
    control = pControl; 
    // Store the delegate in a local field (no problem here)
    _reportResult = reportResult;    
  } 

  private Action<string> _reportResult; 

  private void CommentEntry_Closing(object sender, CancelEventArgs e) { 
    // Invoke the delegate to notify the caller about the value
    _reportResult(tbCommentText.Text.Trim()); 
  } 
} 

因此,现在您的表单可以编辑任何类的注释,这些类表示它知道如何进行注释。

正如Dan Bryant指出的,您不能直接这样做。一种选择是将引用封装到类中,但这需要编写大量样板代码。更简单的选择是使用委托和lambda函数(在C#3.0中)或匿名委托(C#2.0中):

commentry
表单的修改版本如下所示:

// Create the comment Screen
string newCommentText = "";
commentsScreen = new CommentEntry(this, ref newCommentText);

commentScreen.ShowDialog();

...

_dataLayer.SaveOffComment(newCommentText);
public interface ICommented
{
    string Comment { get; set; }
}

public class MyClass : ICommented
{
    public string Comment { get; set; }
}

public partial class CommentEntry : Form
{
    public CommentEntry(Control pControl, ICommented commented)
    {
        InitializeComponent();
        control = pControl;

        // ***** Need a way for this to store the reference not the value. *****
        _commented = commented;
    }


    private ICommented _commented;

    private void CommentEntry_Closing(object sender, CancelEventArgs e)
    {
        _commented.Comment = tbCommentText.Text.Trim();
    }
}
public partial class CommentEntry : Form { 
  public CommentEntry(Control pControl, Action<string> reportResult) { 
    InitializeComponent(); 
    control = pControl; 
    // Store the delegate in a local field (no problem here)
    _reportResult = reportResult;    
  } 

  private Action<string> _reportResult; 

  private void CommentEntry_Closing(object sender, CancelEventArgs e) { 
    // Invoke the delegate to notify the caller about the value
    _reportResult(tbCommentText.Text.Trim()); 
  } 
} 
public分部类CommentEntry:表单{
公共评论条目(控件、控件、操作报告结果){
初始化组件();
控制=控制;
//将代理存储在本地字段中(此处没有问题)
_reportResult=reportResult;
} 
私人行动报告结果;
私有void commentry_Closing(对象发送方,CancelEventArgs e){
//调用委托以通知调用方有关该值的信息
_reportResult(tbCommentText.Text.Trim());
} 
} 

您可以始终将值包装在类中。听起来您想要的是指针,而不是引用。为什么在返回
ShowDialog
后您不能访问类中的任何属性?@Andy,我必须“滚动我自己的”
ShowDialog
方法。它实际上是对
Show
的调用,等待窗口关闭。这会导致对话框中的所有变量在关闭后丢失。查看这个问题了解更多关于为什么我必须滚动我自己的问题的信息:正如我在问题末尾的注释中所说的,在ShowDialog之后,我无法访问类的任何变量