Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 8:57am
rajh7423
Edited 31-Oct-11 8:59am
v3
Rate this: bad
good
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.
 
[Update]
----------
 
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);
}
  Permalink  
v3
Comments
rajh7 at 31-Oct-11 14:21pm
   
How do i add it whats the right syntax please?
Nishant Sivakumar at 31-Oct-11 14:58pm
   
Check my updated answer.
Naerling at 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 at 31-Oct-11 14:52pm
   
It's an out param, so null is fine here.
Naerling at 31-Oct-11 16:40pm
   
Looking better Nish, my 5 :)
And yeah, forgot about that out param.
Espen Harlinn at 31-Oct-11 15:37pm
   
5'ed :)
Monjurul Habib at 31-Oct-11 16:23pm
   
my 5!
Rate this: bad
good
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.
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
// 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! Smile | :)
  Permalink  
v2
Comments
Espen Harlinn at 31-Oct-11 15:37pm
   
5'ed :)
Naerling at 31-Oct-11 16:39pm
   
Thanks Espen :)
Monjurul Habib at 31-Oct-11 16:23pm
   
my 5!
Naerling at 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
0 OriginalGriff 200
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2014
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