Click here to Skip to main content
15,889,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have
C#
Dictionary<int, myClass> ActiveTabIndex = new Dictionary<int, myClass>();

C#
public class myClass
    {
        public int TabIndex { get; set; }
        public string OdSessionId { get; set; }
        public string TextBoxValue { get; set; }
    }


i am adding value to dictionary like
C#
ActiveTabIndex.Add(1, new myClass{ TabIndex = 1, OdSessionId = "S", TextBoxValue=""});


now i want to update TextBoxValue for Key 1,
like
C#
ActiveTabIndex[1] = new myClass{ TextBoxValue="something"};


but its Resting the other value to default,
How Can I update The value of dictionary for the specific key with out overriding Other value.

What I have tried:

C#
ActiveTabIndex[1] = new myClass{ TextBoxValue="something"};

But Is Updating The Other value toi default
Posted
Updated 31-Jul-18 2:06am

1 solution

Because you are creating a new object with new state but you need to just update the state of current object.

You need to do it this way :

C#
ActiveTabIndex[1].TextBoxValue="something";


Now you will have just TextBoxValue property of the instance updated with the other properties value retained.

Hope it helps you understand.
 
Share this answer
 
v3
Comments
Member 11859517 31-Jul-18 10:01am    
how to Retrieve value from the same dictionary
Ehsan Sajjad 31-Jul-18 10:07am    
var textValue = ActiveTabIndex[1].TextBoxValue;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900