Click here to Skip to main content
15,867,991 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm sending a new list(dictionary) by reference to another function in a didderent class where the list adds some strings to it . I also have a private variable called list ( dictionary type)in this class. Now do i have to asign the newlist to the private variable "list" after the newlist added some strings to it? or is the newlist autamatically filling the list varible in this class as its passed as a reference.?

C#
Dictionary<string,> _list;

// creating a new list and to pass by reference to another method in a differerent class.
Dictionary<string,> newList = new Dictionary<string,>();
newList = null;
// passing by reference 
valid = new CaseService().ValidStageTransition(AId, BId, CId, out newList);

// after the above method returns back copy the newlist to the private variable "list"
 _list = new Dictionary<string,>(newList);


is it the right way to do?
Posted
Updated 31-Oct-11 7:59am
v3

If you do that, your original data will be lost. Is that what you want to do?

If you want to merge the two dictionaries, you need to add the contents from the returned collection to the old collection.

[Update]
----------

In response to your comment, and using some imaginary code for those extra classes:

C#
class CaseService
{
    internal bool ValidStageTransition(int p1, int p2, int p3, 
        out Dictionary<string, string> newList)
    {
        newList = new Dictionary<string, string>();
        newList.Add("aaa", "1");
        return true;
    }
}


Here's how you can merge in the extra collection.

C#
Dictionary<string, string> _list = new Dictionary<string,string>();
_list.Add("bbb", "a0");

Dictionary<string, string> newList = null;
bool valid = new CaseService().ValidStageTransition(0, 0, 0, out newList);

foreach (var item in newList)
{
    _list.Add(item.Key, item.Value);
}
 
Share this answer
 
v3
Comments
rajh7 31-Oct-11 14:21pm    
How do i add it whats the right syntax please?
Nish Nishant 31-Oct-11 14:58pm    
Check my updated answer.
Sander Rossel 31-Oct-11 14:39pm    
Whatever the case, the OP is doing some stuff seriously wrong. No need to have two dictionaries here. Also, by assigning a null reference to newList before passing it to the Function this would always cause an Exception. I think the OP simply wants to clear his dictionary here. See my answer for what is 'hopefully' what the OP wants :)
Nish Nishant 31-Oct-11 14:52pm    
It's an out param, so null is fine here.
Sander Rossel 31-Oct-11 16:40pm    
Looking better Nish, my 5 :)
And yeah, forgot about that out param.
Since a Dictionary is a Reference Type[^] you do not have to pass it 'by reference'. Simply passing it to your Method should be enough.

Consider the following:
C#
// Creates a new, empty dictionary.
Dictionary<string, int> dict = new Dictionary<string, int>();
// Simply pass your dictionary to a Method that adds items to it.
FillDictionary(dict);
// Your dictionary will be filled.
int i = dict.Count;

In the above example, if FillDictionary does not remove records from your dictionary, if you pass dict to the FillDictionary Method again you will have twice as much records (if it does not try to add non-unique keys).
If you want to clear your dictionary you can use
C#
// Clears all items from the dictionary, making it empty.
dict.Clear();

What does this mean for you? You can pass _list to your Function directly and all items will be appended. If you wish to have ONLY the records from your Function call _list.Clear(); and then pass it to your Function.
In any case there is no need to create a new dictionary.
Hope it helps! :)
 
Share this answer
 
v2
Comments
Espen Harlinn 31-Oct-11 15:37pm    
5'ed :)
Sander Rossel 31-Oct-11 16:39pm    
Thanks Espen :)
Monjurul Habib 31-Oct-11 16:23pm    
my 5!
Sander Rossel 31-Oct-11 16:39pm    
Thanks :)

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