dictionary

C#: Does a dictionary in C# have something similar to the Python setdefault?

Trying to translate some methods written in Python over into C#. The line looks like this: d[p] = d.setdefault(p, 0) + 1 What exactly does setdefault do? And is there anything similar I can use in a C# dictionary? Or rather, how can I translate that line into C#? ...

Including Large Wordlist in Stand-alone Dictionary Application

The Application I'm working on a simple dictionary search tool, with the main purpose of searching a word-list of around 180,000 words. To begin with, the word-list was a plain-text document, with each word on a single line. Upon loading, the word-list was processed into a simple array for searching. The Objective My objective, howev...

iPhone properties getting set to nil

I set a property with this declaration: @property (nonatomic,retain) NSDictionary *itemData; to a dictionary with 1 key - val pair. The table view in my view then gets the one value for the dictionary fine as a section header. But when I go back to access that very same value, it is nil. Any suggestions? I have been working on this f...

Dictionary<> is reporting invalid key when it's not

I have some code that adds a Func<short> to a Dictionary<byte, Func<short>> at index 0. Later on, some code within the class that contains the dictionary attempts to extract this Func (via TryGetValue) and execute it, throwing an exception if it does not work. Even though the index being accessed is valid, it throws the exception that si...

C# code optimization for accessing datatable records

Hi, i have a cost issue with datatable. And i need to replace the code with a smarter one. i have a datatable and the sample values are like this: Columns : id, user_id, starttime, endtime Row Sample : 1 , 5, 05.10.2009 08:00:00,05.10.2009 17:00 my pseudo code is function something() { for(int i=0;i<datatable.Rows...

Help with C#.NET generic collections performance and optimization

I am trying to optimize a piece of .NET 2.0 C# code that looks like this: Dictionary<myType, string> myDictionary = new Dictionary<myType, string>(); // some other stuff // inside a loop check if key is there and if not add element if(!myDictionary.ContainsKey(currentKey)) { myDictionary.Add(currentKey, ""); } Looks like the Dictio...

WPF binding Dictionary<string, List<string> to listView, ListBox how?

how to bind Dictionary to ListView and Text box? namespace Models { public class DynamicList { #region Constructor public DynamicList() { _propDict = new Dictionary<string, object>(); dynimicListProps = new List<DynimicListProperty>(); } #endregion Constructor #region Fields private Dic...

Does the length of key affect Dictionary performance?

Hi, I will use a Dictionary in a .NET project to store a large number of objects. Therefore I decided to use a GUID-string as a key, to ensure unique keys for each object. Does a large key such as a GUID (or even larger ones) decrease the performance of a Dictionary, e.g. for retrieving an object via its key? Thanks, Andrej ...

Dictionary Property in VBA Class

I have been asked to modify an Excel sheet with some arcaic programming. I have decided to rewrite it rather then modify all of the many GOTO statments and static arrays. My background is in C# so it has been a bit of a challenge (note: I am sure the naming convention is bad, I am used to being able to use underscore to define private ...

c# - Why can't my Dictionary class see the ToArray() method?

Hi, I see in the API Dictionary has a ToArray() method (in the extension classes area), but when I try to use this from my Dictionary instance it can't see it??? How do I "enable" ToArray() for my Dictionary instance? Thanks ...

Java Equivalent to Python Dictionaries

I am a long time user of Python and really like the way that the dictionaries are used. They are very intuitive and easy to use. Is there a good Java equivalent to python's dictionaries? I have heard of people using hashmaps and hashtables. Could someone explain the similarities and differences of using hashtables and hashmaps versus pyt...

Binding Dictionary<,>.Keys.Count to a Label

Hi folks, I have a Dictionary and want to display the number of items in this dictionary with a Label, without updating the label manually each time I add or remove an item from the dictionary. I tried it with the Binding class: Binding bindingNodeCount = new Binding("Text", _graphDisplay.data.nodes.Keys, "Count"); labelNumberOfNodes....

How can 2 Python dictionaries become 1?

Possible Duplicate: Python extend for a dictionary I know that Python list can be appended or extended. Is there an easy way to combine two Python dictionaries with unique keys, for instance: basket_one = {'fruit': 'watermelon', 'veggie': 'pumpkin'} basket_two = {'dairy': 'cheese', 'meat': 'turkey'} I then want one big baske...

How to generate a list of 150 cases initialised with a dict in Python ?

I would like to create a list like this list = [] for i in range(150): list.append({'open': False, 'serve': False}) But is there a better way in Python to do it ? ...

How to strip a list of tuple with python ?

I have an array with some flag for each case. In order to use print the array in HTML and use colspan, I need to convert this : [{'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': False, 'open': False}, {'serve': F...

Sending a Hashtable to a Webservice

Im trying to send a List of Items with generic Keys and Value Types to a asp.net webservice. How can i do this? I tried it with List<Dictionary<String, String>>, but this doens't work. I do not get an exception, but when i break in the webserivice, my collection is null. The Data could look like this: {"Data":{"fieldtype":"integer","isr...

Efficient way to find the largest key in a dictionary with non-zero value

Hi, I'm new Python and trying to implement code in a more Pythonic and efficient fashion. Given a dictionary with numeric keys and values, what is the best way to find the largest key with a non-zero value? Thanks ...

AS3: Check if a Dictionary is empty

Flash implements a dictionary (that is, something like a HashMap) using two approaches. One approach is the flash.utils.Dictionary class, and the other is a generic Object. I'd like to check how many key:value pairs are in the dictionary. In most cases I'd simply like to know it there are any key:value pairs, that is, just check if it's ...

When does Dictionary<TKey, TValue> call TKey.Equals() for custom TKey types?

Just overriding Equals in TKey does not help. public override bool Equals(object obj) { /* ... */ } ... Equals will never be called ... ... adding this doesn't help too: public override int GetHashCode() { return base.GetHashCode(); } BUT, this helps: public override int GetHashCode() { return 0; } Now, I can look up k...

Why cant we change Values of a dictionary while enumerating its keys?

class Program { static void Main(string[] args) { var dictionary = new Dictionary<string, int>() { {"1", 1}, {"2", 2}, {"3", 3} }; foreach (var s in dictionary.Keys) { // Throws the "Collection was modified exception..." on th...