开发者

How to change creating-ref-arguments inside class method?

Referred to the constructor of the class variables that can be assigned to work during this class. How do I make in this class, I could not assign them to in the constructor, but in a different method?

Creating class:

 CreatePackWindow createPackWindow = new CreatePackWindow(ref title, ref开发者_如何学Go description);
 if (createPackWindow.ShowDialog() == true)
 {
    Console.WiteLine(title, description);
 }

Class CreatePackWindow:

public partial class CreatePackWindow : Window
{
     public CreatePackWindow(ref string title, ref string description)
     {
         InitializeComponent();
     }

     private void btnCreate_Click(object sender, RoutedEventArgs e)
     {
            ???title = tbPackName.Text; **// How to assign here?**
            ???description = tbDescription.Text; **// How to assign here?**
            this.DialogResult = true;
            Close();
     }
     //..........
}

I understand that you need to create pointers to title and description and the method to work with them, but do not know how to do it :(

Please help. Thank you.


You can't use ref to make a field act as a pointer; the ref is only in effect during the call. To do what you want, maybe:

  • update the title/description properly (i.e. window.Title = "foo" etc)
  • use a wrapper class as an intermediary - i.e. both uses keep a reference to a class with a title/description

The latter is probably going to be the closest to what you want. i.e. have a

class Foo
{
    public string Title{get;set;}
    public string Description{get;set;}
}

public partial class CreatePackWindow : Window
{
     private readonly Foo foo;
     public CreatePackWindow(Foo foo)
     {
         InitializeComponent();
         this.Foo = foo;
     }

     private void btnCreate_Click(object sender, RoutedEventArgs e)
     {
            foo.Title = tbPackName.Text;
            foo.Description = tbDescription.Text;
            this.DialogResult = true;
            Close();

     }
}

and

var foo = new Foo();
CreatePackWindow createPackWindow = new CreatePackWindow(foo);
 if (createPackWindow.ShowDialog() == true)
 {
    Console.WiteLine(foo.Title, foo.Description);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜