从哈希表中拉出一个对象并将其转换为另一个对象

时间:2016-10-06 08:45:50

标签: c#

我有Hashtable名为hash,其第一个值是GUID,第二个是InstallationFiles类型的对象。我希望能够做到以下几点:

    InstallationFiles ifiles = (InstallationFiles)hash[objectGuid];

即使这个返回正确的值hash[objectGuid],我也会在Hashtable中找到hash[objectGuid]作为键的条目,但是这个(InstallationFiles)hash[objectGuid]总是返回null。为什么会这样?

更新。

好的,我会显示更多代码。 用户在此窗口中。他/她单击添加文件按钮以将文件添加到列表中。 enter image description here

弹出另一个窗口:enter image description here

当用户点击第一个表单(第一张图片)中的添加文件按钮时,会触发以下方法:

Setup_Generator.cs(我的主要表单对象,第一张图片):

    private void addFileOrDirectory(string ifFileOrDirectory)
    {            
           if (ifFileOrDirectory == "File")
           {
             //here I initialize the object of the second form (second picture)
              efef = new editFileEntryForm(info.TheRightProgramFilesDirectoryUserView, info.ApplicationName, "File", null);
            if (efef.ShowDialog() == DialogResult.OK)
            {
               //every file entry has a unique guid
                Guid g = Guid.NewGuid();
                //efef.InstFiles is an object of type InstallationFiles (code in the next block)
                hash.Add(g, efef.InstFiles);
                item1 = new ListViewItem(efef.InstFiles.File);
                item1.SubItems.Add(efef.InstFiles.DestDir);
                listViewDerivative1.Items.AddRange(new ListViewItem[] { item1 });
                item1.Tag = g;
            }
        }

editFileEntryForm.cs,或第二张图片中显示的第二种形式:

    class editFileEntryForm 
    {
    public Classes.InstallationFiles InstFiles = null;
    //...
    public editFileEntryForm(string instDir, string appName, string fileOrDir, Classes.InstallationFiles instFiles) 
    {
     //...
        if (instFiles != null)
        {
          InstFiles = instFiles;
          sourceFileTextBox.Text = instFiles.File;
          this.destinationDirComboBox.SelectedIndex = instFiles.DestinationDirSelectedIndx;
          this.ifFileExistsComboBox.SelectedIndex = instFiles.FileExistsSelectedIndex;
        }
    }
    private void okButtonClick() 
    {
      if (FileOrDir == "File") 
        {
            //here I initialize an object which I add to a hashtable in Setup_generator.cs
            InstFiles = new Classes.InstallationFiles(this.sourceFileTextBox.Text, 
                                                      destinationDirComboBox.Text,
                                                      NSISvariables[destinationDirComboBox.Text],
                                                      NSISvariables[ifFileExistsComboBox.Text],
                                                      ifFileExistsComboBox.SelectedIndex,
                                                      destinationDirComboBox.SelectedIndex
                                                      );
            this.DialogResult = DialogResult.OK;
            this.Hide();
        }
    }
    }

到目前为止,一切都进入计划 - 值保存在Hashtable哈希中,键是GUID,值是InstallationFiles对象。当用户选择一个条目并在主窗体中单击编辑(Setup_Generator.cs或第一张图片)时,我会执行以下操作:

    private void edit()
    {
        //retrieve the selected item and its GUID
        ListViewItem lvi = listViewDerivative1.SelectedItems[0];
        string objectGuid = lvi.Tag.ToString();
        //(Classes.InstallationFiles)hash[objectGuid] returns null, even though an object with the given GUID exists in the hashtable!
        Classes.InstallationFiles ifiles = (Classes.InstallationFiles)hash[objectGuid];
        //initializing a new form to edit a selected entry
        efef = new    editFileEntryForm(this.info.TheRightProgramFilesDirectoryUserView, this.info.ApplicationName, "File", ifiles);
        if (efef.ShowDialog() == DialogResult.OK)
        {
           //This is my ultimate goal: replace old values with new ones in the HASHTABLE hash!
           //not working, because hash[objectGuid] as Classes.InstallationFiles) returns as null
           (hash[objectGuid] as Classes.InstallationFiles).File = efef.InstFiles.File;
           (hash[objectGuid] as Classes.InstallationFiles).DestDir = efef.InstFiles.DestDir;     
           (hash[objectGuid] as Classes.InstallationFiles).DestDirNSISVariable = efef.InstFiles.DestDirNSISVariable;
           (hash[objectGuid] as Classes.InstallationFiles).IfFileExistsNSISVariable = efef.InstFiles.IfFileExistsNSISVariable;
        {    
        }
        }

1 个答案:

答案 0 :(得分:1)

您正在致电

efef = new editFileEntryForm(info.TheRightProgramFilesDirectoryUserView,
       info.ApplicationName, "File", null);

但您将instFiles作为空

传递
public editFileEntryForm(string instDir, string appName, string fileOrDir, Classes.InstallationFiles instFiles) 
{
 //... instFiles - null here
    if (instFiles != null)
    {
      InstFiles = instFiles;
      sourceFileTextBox.Text = instFiles.File;
      this.destinationDirComboBox.SelectedIndex = instFiles.DestinationDirSelectedIndx;
      this.ifFileExistsComboBox.SelectedIndex = instFiles.FileExistsSelectedIndex;
    }
}

因此,当您致电hash.Add(g, efef.InstFiles);时,您将null值传递为efef.InstFiles

这就是为什么哈希表包含空值。