Click here to Skip to main content
13,044,115 members (66,960 online)
Rate this:
Please Sign up or sign in to 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.?

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 31-Oct-11 7:57am
Updated 31-Oct-11 7:59am
Nish Nishant648.6K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.


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

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.

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);
rajh7 31-Oct-11 14:21pm
How do i add it whats the right syntax please?
Nishant Sivakumar 31-Oct-11 14:58pm
Check my updated answer.
Naerling 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 :)
Nishant Sivakumar 31-Oct-11 14:52pm
It's an out param, so null is fine here.
Naerling 31-Oct-11 16:40pm
Looking better Nish, my 5 :)
And yeah, forgot about that out param.
Espen Harlinn 31-Oct-11 15:37pm
5'ed :)
Monjurul Habib 31-Oct-11 16:23pm
my 5!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:
// 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.
// 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
// Clears all items from the dictionary, making it empty.

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! :)
Espen Harlinn 31-Oct-11 15:37pm
5'ed :)
Naerling 31-Oct-11 16:39pm
Thanks Espen :)
Monjurul Habib 31-Oct-11 16:23pm
my 5!
Naerling 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100