Gopal Reddy V
Gopal Reddy V

Reputation: 365

initialize dictionary values from another class

I have a 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;
    }


}


Now i add to add values to details dictionary

that is i need to add all the input values from the form and in my webform i tried some thing like this

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



    }


Now I neeed to assign Values dictionary to details dictionary which is in another class

Upvotes: 0

Views: 918

Answers (4)

Farhad Jabiyev
Farhad Jabiyev

Reputation: 26635

You can use constructor which takes Dictionary as parameter or implement the setDictionary method or use public auto-implemented Property which creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

@Darin showed how to make first one, I am showing the last one:

Here is the syntax of auto-implemented Property:

public PropetyName{ get; set;}

If you want read-only or write-only property, then you can use:

 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 

So, you can change your code as:

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

Then you can use:

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

or

ClsDictionary dict = new ClsDictionary(Values);

Upvotes: 1

Florian Schmidinger
Florian Schmidinger

Reputation: 4692

Yet another way:

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

Then add with:

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

}

The AddDetail Method would return if the value is not added allready ... another way to implement is:

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

This would alter the Detail if its allready in the dictionary or add another value.

Upvotes: 0

ASh
ASh

Reputation: 35646

to add values to details dictionary

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

you can also try this, if you can change 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);
}

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could have your ClsDictionary class take it as a constructor argument:

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

and then:

ClsDictionary dict = new ClsDictionary(Values);

Alternatively you could have a method that will clear the original dictionary and replace it with the new values:

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

and then:

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

Upvotes: 1

Related Questions