从另一个类初始化字典值

时间:2015-02-20 07:18:23

标签: c# dictionary

我有一个班级名称clsDictionary .....

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

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

    }

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


}


现在我添加了向细节字典添加值

这是我需要添加表单中的所有输入值,在我的webform中我尝试过这样的事情

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



    }


现在我需要将值词典分配给另一个类中的详细信息词典

4 个答案:

答案 0 :(得分:1)

您可以将ClsDictionary类视为构造函数参数:

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

答案 1 :(得分:1)

将值添加到详细信息词典

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

答案 2 :(得分:1)

您可以使用以Dictionary作为参数或实施setDictionary方法的构造函数,或使用 公开自动实现的属性 来创建私有,匿名支持字段,只能通过属性的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);

答案 3 :(得分:0)

另一种方式:

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方法将返回...另一种实现方式是:

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

如果详细信息已在字典中显示或添加其他值,则会改变详细信息。