-4

我有一个包含结构作为键的字典,我必须为字典创建一个属性

项目1

namespace ClassLibrary2
{
  public class Class1
  {
    public Dictionary<string, Cs> mdic;
    public Class1()
    {
        mdic = new Dictionary<string, Cs>();
        mdic.Add("Welcome", new Cs() { m1 = "12",m2="32"});
    }

    public Dictionary<string, Cs> Dic
    {
        get {return mdic;}
        set { value = mdic; }
    }

    public struct Cs
    {
        public string m1{get;set;}
        public string m2{get;set;}
    }
}

当我尝试设置此值时,project 1它显示错误...

像这样我设置

项目2

namespace WindowsFormsApplication20
{
  public partial class Form1 : Form
  {
     public Form1()
     {
        InitializeComponent();
     }

     private Dictionary<string, Cs> Dic
     {
        get;
        set;
     }

     struct Cs
     {
         public string m1 { get; set; }
         public string m2 { get; set; }
     }

     private void button1_Click(object sender, EventArgs e)
     {
        ClassLibrary2.Class1 css = new ClassLibrary2.Class1();
        Dic = css.Dic;
     }
 }

我有问题 css.Dic ..... System Generic Collections

4

3 回答 3

2

ClassLibrary2.Class1.Cs 结构和 WindowsFormsApplication20.Form1.Cs 结构是 2 种不同的类型,因此您不能将 a 分配给Dictionary<string, ClassLibrary2.Class1.Cs>type 的变量Dictionary<string, WindowsFormsApplication20.Form1.Cs>。尝试从 Form1 中删除 Cs 声明,如下所示:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private Dictionary<string, ClassLibrary2.Class1.Cs> Dic
    {
        get;
        set;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ClassLibrary2.Class1 css = new ClassLibrary2.Class1();
        Dic = css.Dic;
    }
}
于 2012-10-25T11:20:44.097 回答
0

Your dictionary definition in Form1 should be -

private Dictionary<string, ClassLibrary2.Class1.Cs> Dic
{
    get;
    set;
}
于 2012-10-25T11:22:40.483 回答
0

您已经在您的 privious 问题评论中收到了答案。

你的ClassLibrary2.Class1.CSWindowsFormsApplication20.Form1.CS是不同的结构。

在 WindowsFormsApplication20 中,您不应声明新的 CS 结构。只需使用 ClassLibrary2 命名空间。

using ClassLibrary2;

namespace WindowsFormsApplication20
{
  public partial class Form1 : Form
  {
     public Form1()
     {
        InitializeComponent();
     }

     private Dictionary<string, Class1.Cs> Dic
     {
        get;
        set;
     }

     private void button1_Click(object sender, EventArgs e)
     {
        ClassLibrary2.Class1 css = new ClassLibrary2.Class1();
        Dic = css.Dic;
     }
 }
于 2012-10-25T11:24:19.700 回答