简体   繁体   English

是否有标准方法可以在类之间引用相同的 object?

[英]Is there a standard way to reference the same object between classes?

When I search this question, all of the answers I've seen only talk about handling an object within the same class but I need to handle the same data between classes.当我搜索这个问题时,我看到的所有答案都只讨论在同一个 class 中处理 object 但我需要在类之间处理相同的数据。

I'm building an outlook addin which uses three classes to make up the key components of my programme: ThisAddin - the default class created with the MS VS template, I'm using this class to store all of my methods, enums, data stores etc.. Ribbon - my outlook Ribbon containing a button to initiate a new enquiry form.我正在构建一个 outlook 插件,它使用三个类来构成我的程序的关键组件: ThisAddin - 使用 MS VS 模板创建的默认 class,我正在使用这个 ZA2F2ED4F8EBC2CBB4C21A29DZ 方法来存储所有数据,等等。功能区- 我的 outlook 功能区包含一个按钮,用于启动新的查询表单。 Within the button_click is also a code to save relevant attachments from a selected email to a specified filepath.在 button_click 中还有一个代码,用于将相关附件从选定的 email 保存到指定的文件路径。 NewEntry - a winform dialogue initiated when the ribbon button is clicked, used to create new enquiries. NewEntry - 单击功能区按钮时启动的 winform 对话框,用于创建新查询。

I also have a class called NewSearchQuery which I was hoping to use as a datastore for all the relevant aspects of a new enquiry.我还有一个名为NewSearchQuery的 class,我希望将其用作新查询的所有相关方面的数据存储。

current logic goes like this - user presses button in the ribbon, a new object (referenced as Temp) is called and known data for some parameters are filled.当前逻辑是这样的 - 用户按下功能区中的按钮,调用新的 object(称为 Temp)并填充某些参数的已知数据。 Winform is opened and user fills in some more required data. Winform 被打开,用户填写一些更多需要的数据。 On form submit, that data is added to the object metadata, a reference number is generated from a database, a file path is created using the reference number and all this is again added to the object metadata.在提交表单时,该数据被添加到 object 元数据中,从数据库生成参考编号,使用参考编号创建文件路径,所有这些再次添加到 object 元数据中。 Winform dialog closes and the ribbon saves the selected email attachments to the specified filepath stored within the object. Winform 对话框关闭,功能区将选定的 email 附件保存到 object 中存储的指定文件路径。 Now all the object metadata can be sent to the enquiry database as a new enquiry.现在所有 object 元数据都可以作为新查询发送到查询数据库。

Here's the code:这是代码:

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();
        }
    }
}

How can I reference the same object from both the Ribbon and NewEntry classes to keep all my required data centralised?如何从 Ribbon 和 NewEntry 类中引用相同的 object 以集中所有需要的数据?

It looks like you don't need to actually share and you only want NewEntry to give you a NewSearchQuery when it completes.看起来您不需要实际共享,您只希望 NewEntry 在完成时给您一个 NewSearchQuery。

That is pretty easy just add a method on NewEntry like this:这很简单,只需在 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;
}

Then in the ribbon just do this after the ShowDialog:然后在功能区中,在 ShowDialog 之后执行此操作:

var q = NewEntry.GetEntry();

So the idea is that the NewSearchQuery is always created by the form.所以想法是 NewSearchQuery 总是由表单创建的。

If you want a 2 way thing, create the NewSearchQuery first, then pass to the form using a similar method that initializes the form from the values.如果你想要一个 2 方式的东西,首先创建 NewSearchQuery,然后使用从值初始化表单的类似方法传递给表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM