Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Dictionary<string,> newList = new Dictionary<string,>();
newList.add("cat", "dog");
From the above code I'm trying to initialize the dictionary list. coz in my case I pass it to a function where it get filled and comes back.
 
Some times it comes back as emty list so when i use it, its throwing null exception.
 
So I want to initialize the list before i pass it to the function. To do that I added two values to the list assuming its an initialization, but its not.
 
Could you please give me the right code for initialization.
 
If I initialize before a pass it, would it solve the problem or where should i initialize it?
Posted 2-Nov-11 6:24am
rajh7423
Edited 2-Nov-11 6:29am
RaisKazi33.1K
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Once you call new it's already initialized. What you are probably seeing is that you are assuming that the function populates at least n entries into the collection, and when you access those elements blindly, you will run into null exceptions. Make sure you check for things like count, whether a key exists etc. And if you still have trouble, post more code (minimal repro) and someone'll be able to help you out.
  Permalink  
v2
Comments
hzawary at 2-Nov-11 11:34am
   
Sorry, may you see problem me?
The "How to resizing a large image of ImageBrush object to small under WPF?" in Questions.

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

  Print Answers RSS
0 OriginalGriff 275
1 Maciej Los 230
2 Sergey Alexandrovich Kryukov 185
3 Afzaal Ahmad Zeeshan 152
4 ProgramFOX 130
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 2 Nov 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