将值从内部方法传递到另一个类

时间:2015-09-29 19:17:39

标签: c# string methods pass-by-reference

我试图传递价值"测试"从类WorkspaceNoteUpdateComponent内部和方法noteUpdateControl_AddNoteClicked中,到另一个类NoteUpdateUserControl和方法buttonAdd_Click。然而,出于某种原因,似乎"测试"的价值。在第一种方法中永远不会让它传递出去。我做错了什么?

类WorkspaceNoteUpdateComponent,方法noteUpdateControl_AddNoteClicked

public class WorkspaceNoteUpdateComponent : IWorkspaceComponent2
{
    private IRecordContext _recContext;
    public static string CustAddrCity = "SOMETHING";


    public Control GetControl()
    {
        NoteUpdateUserControl noteUpdateControl = new NoteUpdateUserControl();
        noteUpdateControl.AddNoteClicked += new NoteUpdateUserControl.AddNoteHandler(noteUpdateControl_AddNoteClicked);

        return noteUpdateControl;
    }

    public string noteUpdateControl_AddNoteClicked(ref string CustAddrCity)
    {
        IContact contactRecord = _recContext.GetWorkspaceRecord(RightNow.AddIns.Common.WorkspaceRecordType.Contact) as IContact;

        CustAddrCity = "test";

        _recContext.RefreshWorkspace();
        return CustAddrCity;
    }
}

Class NoteUpdateUserControl with method buttonAdd_Click

public partial class NoteUpdateUserControl : UserControl
{
    public delegate string AddNoteHandler(ref string custAddrCity);
    public event AddNoteHandler AddNoteClicked;
    string _boxtext = WorkspaceNoteUpdateComponent.CustAddrCity;

    public NoteUpdateUserControl()
    {
        InitializeComponent();
    }

    private void buttonAdd_Click(object sender, EventArgs e)
    {

        if (AddNoteClicked != null)
        {


            MessageBox.Show(_boxtext);
        }
    }

*** UPDATE从字符串_boxtext中删除了readonly,因为这不是代码的预期部分****

1 个答案:

答案 0 :(得分:0)

您是否在其他任何地方触发了您的活动?

您的代码缺少事件触发:

    if (AddNoteClicked != null)
    {
        string _eventBoxText = _boxtext;

        AddNoteClicked(ref _eventBoxText);  //<-- YOU NEED THIS LINE TO TRIGGER YOUR EVENT

        MessageBox.Show(_eventBoxText );
    }

注意我已将_boxtext替换为_eventBoxText_boxtext只读取would not be acceptable as ref argument in any context other method than the class constructor

相关问题