【问题标题】:Is there a standard way to reference the same object between classes?是否有一种标准方法可以在类之间引用相同的对象?
【发布时间】:2021-02-04 10:33:13
【问题描述】:

当我搜索这个问题时,我看到的所有答案都只谈论在同一个类中处理一个对象,但我需要在类之间处理相同的数据。

我正在构建一个 Outlook 插件,它使用三个类来构成我的程序的关键组件: ThisAddin - 使用 MS VS 模板创建的默认类,我使用这个类来存储我所有的方法、枚举、数据存储等。 功能区 - 我的 Outlook 功能区,其中包含一个用于启动新查询表单的按钮。在 button_click 中还有一个代码,用于将选定电子邮件中的相关附件保存到指定的文件路径。 NewEntry - 单击功能区按钮时启动的 winform 对话框,用于创建新查询。

我还有一个名为 NewSearchQuery 的类,我希望将其用作新查询的所有相关方面的数据存储。

当前逻辑是这样的 - 用户按下功能区中的按钮,调用一个新对象(引用为 Temp)并填充某些参数的已知数据。 Winform 被打开,用户填写一些更多需要的数据。在表单提交时,该数据被添加到对象元数据中,从数据库生成参考号,使用参考号创建文件路径,所有这些都再次添加到对象元数据中。 Winform 对话框关闭,功能区将选定的电子邮件附件保存到对象中存储的指定文件路径。现在可以将所有对象元数据作为新查询发送到查询数据库。

代码如下:

public enum ContractType
{
    Searches, //there are other values in my code but they don't need to feature here
}

public class NewSearchQuery
{
    public string Name, Location, SearchType, Path;
    public int RefNum;
    public bool Paid;
    public ContractType Contract;
}

public partial class Ribbon
{

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        NewSearchQuery Temp = new NewSearchQuery();
        Temp.Contract = ContractType.Searches;
        var NewEntry = new NewEntry(Convert.ToString(Temp.Contract));
        NewEntry.ShowDialog();

        //wait until the NewEntry Dialogue and associated methods close and then run the below save attachments method:

        var m = e.Control.Context as Inspector;
        var mailitem = m.CurrentItem as MailItem;
        mailitem.SaveAs(Temp.Path + @"\Initial_Contact.msg");
        if (mailitem != null)
        {
            if (mailitem.Attachments.Count > 0)
            {
                foreach (Attachment item in mailitem.Attachments)
                {
                    string[] extensionsArray = { ".jpg", ".jpeg", ".gif", ".png", ".tiff", ".tif", ".eps", ".bmp", ".vcf" };
                    if (!extensionsArray.Any(item.FileName.Contains))
                    {
                        item.SaveAsFile(Path.Combine(Temp.Path, item.FileName));
                    }
                }
            }
            else
            {
                MessageBox.Show($"Operation Complete. Enquiry number {Temp.RefNum}. This email doesn't have any attachments.");
            }
        }
        MessageBox.Show($"Operation Complete, Enquiry number {Temp.RefNum}.");
    }
}
public partial class NewEntry : Form
{
    public NewEntry(string ContractType)
    {
        InitializeComponent();
    }

    private void Create_Click(object sender, EventArgs e)
    {
        //here are the variables I would like to tie into the Temp object created in Ribbon
        Temp.Name = Convert.ToString(Companies.SelectedItem);
        Temp.Location = Convert.ToString(SearchLocation.Text);
        Temp.SearchType = Convert.ToString(Search.SelectedItem);
        Temp.Paid = Paid.Checked;

        if (Temp.Name == "" || Location == "" || SearchType == "")
        {
            MessageBox.Show("Please ensure you have filled in all the required fields (*) before proceeding", "ERROR: insufficient info");
        }
        else
        {

            Temp.RefNum = ThisAddIn.FindIdNum();
            Temp.Path = ThisAddIn.CreateFilePath(Temp.RefNum);
            MessageBox.Show(Convert.ToString(d));
            this.Close();
        }
    }
}

如何从 Ribbon 和 NewEntry 类中引用同一个对象以集中所有需要的数据?

【问题讨论】:

  • 将逻辑封装在接口(例如 ISearchQueryService)中,并将该接口的实例传递给想要使用它的两个类。

标签: c# winforms oop object outlook


【解决方案1】:

您似乎不需要实际分享,您只希望 NewEntry 在完成时为您提供一个 NewSearchQuery。

这很简单,只需像这样在 NewEntry 上添加一个方法:

public NewSearchQuery GetEntry()
{
    // make a new NewSearchQuery
    var q = new NewSearchQuery();
    // set its values from the form eg 
    q.Name = Convert.ToString(Companies.SelectedItem);
    return q;
}

然后在功能区中,在 ShowDialog 之后执行此操作:

var q = NewEntry.GetEntry();

所以这个想法是 NewSearchQuery 总是由表单创建的。

如果你想要一个 2 方式的东西,首先创建 NewSearchQuery,然后使用从值初始化表单的类似方法传递给表单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    相关资源
    最近更新 更多