从另一个 class 初始化字典值

initialize dictionary values from another class

我有一个 class names clsDictionary.....

public class ClsDictionary
{ 
    private Dictionary<string, string> Details;

    public  ClsDictionary()
    {
        Details = new Dictionary<string, string>();

    }

    public Dictionary<string, string> getDictionary()
    {
        return this.Details;
    }


}


现在我添加到详细信息字典中添加值

也就是说,我需要添加表单中的所有输入值,在我的网络表单中,我尝试了类似这样的操作

protected void btnPayment_Click(object sender, EventArgs e)
    {

         Dictionary<string, string> Values = new Dictionary<string, string>();
        Values.Add("Email", txtEmail.Text);
        Values.Add("FirstName", txtFname.Text);
        Values.Add("LastName", txtLname.Text);
        Values.Add("Address1", txtAddress1.Text);
        Values.Add("Address2", txtAddress2.Text);
        Values.Add("City", txtCity.Text);
        Values.Add("State", txtState.Text);
        Values.Add("Country", txtCountry.Text);
        Values.Add("PinCode", txtPincode.Text);
        Values.Add("Phone", txtPhone.Text);
        Values.Add("Country", txtCountry.Text);

        ClsDictionary dict = new ClsDictionary();



    }


现在我需要将值字典分配给另一个 class

中的详细信息字典

您可以让您的 ClsDictionary class 将其作为构造函数参数:

public class ClsDictionary
{ 
    private Dictionary<string, string> Details;

    public ClsDictionary() 
        : this(new Dictionary<string, string>())
    {
    }

    public ClsDictionary(Dictionary<string, string> details)
    {
        this.Details = details;
    }

    public Dictionary<string, string> getDictionary()
    {
        return this.Details;
    }
}

然后:

ClsDictionary dict = new ClsDictionary(Values);

或者,您可以使用一种方法来清除原始字典并将其替换为新值:

public void UpdateDetails(Dictionary<string, string> details)
{
    this.Details = new Dictionary<string, string>(details);
}

然后:

ClsDictionary dict = new ClsDictionary();
dict.UpdateDetails(Values);

向详细信息字典添加值

protected void btnPayment_Click(object sender, EventArgs e)
{
    ClsDictionary dict = new ClsDictionary();
    Dictionary<string, string> Values = dict.getDictionary();
    Values.Add("Email", txtEmail.Text);
    Values.Add("FirstName", txtFname.Text);
    Values.Add("LastName", txtLname.Text);
    Values.Add("Address1", txtAddress1.Text);
    Values.Add("Address2", txtAddress2.Text);
    Values.Add("City", txtCity.Text);
    Values.Add("State", txtState.Text);
    Values.Add("Country", txtCountry.Text);
    Values.Add("PinCode", txtPincode.Text);
    Values.Add("Phone", txtPhone.Text);
    Values.Add("Country", txtCountry.Text);
}

你也可以试试这个,如果你能改变 ClsDictionary

public class ClsDictionary
{ 
    public  ClsDictionary()
    {
        Details = new Dictionary<string, string>();

    }

    public Dictionary<string, string> Details
    {
        get; private set;
    }
}

protected void btnPayment_Click(object sender, EventArgs e)
{
    ClsDictionary dict = new ClsDictionary();
    Dictionary<string, string> Values = dict.Details;
    Values.Add("Email", txtEmail.Text);
    Values.Add("FirstName", txtFname.Text);
    Values.Add("LastName", txtLname.Text);
    Values.Add("Address1", txtAddress1.Text);
    Values.Add("Address2", txtAddress2.Text);
    Values.Add("City", txtCity.Text);
    Values.Add("State", txtState.Text);
    Values.Add("Country", txtCountry.Text);
    Values.Add("PinCode", txtPincode.Text);
    Values.Add("Phone", txtPhone.Text);
    Values.Add("Country", txtCountry.Text);
}

您可以使用以 Dictionary 作为参数的构造函数或实现 setDictionary 方法或使用 public 自动实现 属性 创建一个私有的匿名支持字段,只能通过 属性 的 getset 访问器访问。

@Darin 展示了如何制作第一个,我展示的是最后一个:

这是自动实现的语法 属性:

public PropetyName{ get; set;}

如果你想要只读或者只写属性,那么你可以使用:

 public PropetyName{ get; private set; } // Readonly in outside of the class implementation
 public PropetyName{ private get; set; } // Writeonly in outside of the class implementation
 public PropetyName{ get; } // Readonly
 public PropetyName{ set; } // Writeonly 

因此,您可以将代码更改为:

public class ClsDictionary
{ 
    public Dictionary<string, string> Details { private get; set; }

    public  ClsDictionary()
    {
        Details = new Dictionary<string, string>();
    }

    public  ClsDictionary(Dictionary<string, string> details)
    {
        Details = details;
    }
}

那么你可以使用:

ClsDictionary dict = new ClsDictionary();
dict.Details = Values;

ClsDictionary dict = new ClsDictionary(Values);

另一种方式:

public class ClsDictionary
{
    private Dictionary<string, string> Details;

    public ClsDictionary()
    {
        Details = new Dictionary<string, string>();

    }

    public Dictionary<string, string> getDictionary()
    {
        return this.Details;
    }

    public bool AddDetail(string name, string value)
    {
        if (this.Details.ContainsKey(name))
        {
            return false;
        }
        this.Details.Add(name, value);
        return true;
    }
}

然后添加:

protected void btnPayment_Click(object sender, EventArgs e)
{
    ClsDictionary dict = new ClsDictionary();
    dict.AddDetail("Email", txtEmail.Text);
    dict.AddDetail("FirstName", txtFname.Text);
    dict.AddDetail("LastName", txtLname.Text);
    dict.AddDetail("Address1", txtAddress1.Text);
    dict.AddDetail("Address2", txtAddress2.Text);
    dict.AddDetail("City", txtCity.Text);
    dict.AddDetail("State", txtState.Text);
    dict.AddDetail("Country", txtCountry.Text);
    dict.AddDetail("PinCode", txtPincode.Text);
    dict.AddDetail("Phone", txtPhone.Text);
    dict.AddDetail("Country", txtCountry.Text);

}

AddDetail 方法将 return 如果该值未全部添加...另一种实现方式是:

public void SetDetail(string name, string value)
{
    if (this.Details.ContainsKey(name))
    {
        this.Details[name] = value;
    }
    else
    {
        this.Details.Add(name, value);
    }
}

如果详细信息已在字典中或添加其他值,这将更改详细信息。