Click here to Skip to main content
15,886,806 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Dictionary Misuse Pin
PIEBALDconsult9-Jul-12 10:55
mvePIEBALDconsult9-Jul-12 10:55 
GeneralRe: Dictionary Misuse Pin
Harley L. Pebley10-Jul-12 5:16
Harley L. Pebley10-Jul-12 5:16 
GeneralRe: Dictionary Misuse Pin
Martin Thwaites10-Jul-12 5:24
Martin Thwaites10-Jul-12 5:24 
GeneralRe: Dictionary Misuse Pin
Brisingr Aerowing10-Jul-12 6:16
professionalBrisingr Aerowing10-Jul-12 6:16 
AnswerRe: Dictionary Misuse Pin
Harley L. Pebley10-Jul-12 6:21
Harley L. Pebley10-Jul-12 6:21 
GeneralRe: Dictionary Misuse Pin
James Lonero11-Jul-12 4:57
James Lonero11-Jul-12 4:57 
GeneralRe: Dictionary Misuse Pin
Florin Jurcovici12-Jul-12 22:02
Florin Jurcovici12-Jul-12 22:02 
GeneralRe: Dictionary Misuse Pin
ely_bob10-Jul-12 9:55
professionalely_bob10-Jul-12 9:55 
Martin Thwaites wrote:

string keyToFind = "asdfgsd44rtgr";
 
Dictionary<string, object> dctTemp = GetDictionary();
 
object theObject = dctTemp.ToList().FirstOrDefault(x => x.key == keyToFind).Value;

Should be:
C#
object theObject = GetDictionary()[keytoFind];


will return null if it's not in there, and with a dictionary you can only add one unique Key per...See:
C#
using System.Collections.Generic;

namespace ConsoleApplication2
{
	class Program
	{
		static void Main(string[] args)
		{
			Class1.Sample();
		}
		static class Class1
		{
			private static Dictionary GetDictionary()
			{
				var retval = new Dictionary();
				retval.Add("asdfgsd44rtgr", "1");

				//Throws Error
				//System.ArgumentException was unhandled
				//Message=An item with the same key has already been added.
				//Source=mscorlib
				//StackTrace: {ommitted}
				retval.Add("asdfgsd44rtgr", "throws error");
				return retval;
			}
			public static void Sample()
			{
				string keyToFind = "asdfgsd44rtgr";

				Dictionary dctTemp = GetDictionary();

				//object theObject = dctTemp.ToList().FirstOrDefault(x => x.Key == keyToFind).Value;
				object theObject = GetDictionary()[keyToFind];
			}


		}

	}
}


or did I miss Something ?Confused | :confused:

I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

-----

"The conversations he was having with himself were becoming ominous."-.. On the radio...




GeneralGreat implementation Pin
cansino6-Jul-12 2:53
cansino6-Jul-12 2:53 
GeneralRe: Great implementation Pin
Bernhard Hiller6-Jul-12 4:27
Bernhard Hiller6-Jul-12 4:27 
GeneralRe: Great implementation Pin
PIEBALDconsult6-Jul-12 5:40
mvePIEBALDconsult6-Jul-12 5:40 
GeneralRe: Great implementation Pin
cansino8-Jul-12 21:19
cansino8-Jul-12 21:19 
GeneralRe: Great implementation Pin
OriginalGriff8-Jul-12 21:46
mveOriginalGriff8-Jul-12 21:46 
GeneralRe: Great implementation Pin
Henning Dieterichs9-Jul-12 5:45
Henning Dieterichs9-Jul-12 5:45 
JokeToString() and ToUpper() are like, so cool! Pin
Nathan D Cook5-Jul-12 9:27
Nathan D Cook5-Jul-12 9:27 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
CDP18025-Jul-12 10:17
CDP18025-Jul-12 10:17 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
OriginalGriff5-Jul-12 21:03
mveOriginalGriff5-Jul-12 21:03 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
Gary R. Wheeler6-Jul-12 1:47
Gary R. Wheeler6-Jul-12 1:47 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
OriginalGriff6-Jul-12 2:47
mveOriginalGriff6-Jul-12 2:47 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
Gary R. Wheeler6-Jul-12 3:16
Gary R. Wheeler6-Jul-12 3:16 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
TNCaver10-Jul-12 5:35
TNCaver10-Jul-12 5:35 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
V.6-Jul-12 1:53
professionalV.6-Jul-12 1:53 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
Nagy Vilmos6-Jul-12 2:21
professionalNagy Vilmos6-Jul-12 2:21 
GeneralRe: ToString() and ToUpper() are like, so cool! Pin
PIEBALDconsult6-Jul-12 3:48
mvePIEBALDconsult6-Jul-12 3:48 
Generalgot another codebase to work on... its full of surprises!!! Pin
Ankush Bansal4-Jul-12 23:45
Ankush Bansal4-Jul-12 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.