Questions tagged [trygetvalue]

"TryGetValue" usually refers to an operation of value lookup at dictionary (or map) by a specified key. "Try" here means that it is a failsafe method, i.e. in case of the key is not contained by the dictionary, some indicator of missing value is returned.

When tagging a question with , be sure to tag it with the language being used, and with the related data structure, like , , , , , , or .

In C++

std::map<Key, T, Compare, Allocator>::find: returns an iterator, which is empty in case of no such key.

In .NET

Dictionary<TKey, TValue>.TryGetValue: returns true or false depending on whether the provided key was found and a value as an out parameter.

In Python

dict.get: Returns the value, which is substituted by a provided default in case of no such a key.

In Java

Map<K, V>.get: Returns the value or null if this map contains no mapping for the key.


For questions about Mapping Functions over collections of data, Please Use tag.

56 questions
1
vote
2 answers

Why is TryGetValue on a Dictionary who's key is also a dictionary returning Null?

Goal: get a value from a dictionary. Said value has a dictionary as a key. What I'm doing: I'm creating a second dictionary that has the exact same values as the key whose value I'm trying to get. Using TryGetValue Result: Expecting a value but…
1
vote
1 answer

Is bool ConcurrentDictionary.TryGetValue(TKey, out TValue) method thread-safe?

It's not quite clear from this post: https://devblogs.microsoft.com/pfxteam/concurrentdictionarys-support-for-adding-and-updating/
Andrei Sedoi
  • 1,534
  • 1
  • 15
  • 28
1
vote
1 answer

TryGetValue gives incorrect result

I am trying to get the value from Dictionary(JSON deserialized) and parse it as long. When I quick viewed the variable I found there is no "M" as part of the out parameter a s given below But when I click into the value, I found "M" being added to…
Gopi
  • 5,656
  • 22
  • 80
  • 146
1
vote
1 answer

Resharper Refactor pattern for TryGetValue in Dictionary

The most cumbersome piece of syntax I need to continually write is lazily initializing a container in a dictionary like: ContainerRecord value; if(!_dictionary.TryGetValue(key,out value)) { value = new ContainerRecord(); _dictionary[key] =…
Spencer Rose
  • 1,190
  • 12
  • 21
1
vote
3 answers

Less wrong TryGetValue then null check syntax

I'm a little new to C# still... I'm finding myself reusing a specific procedure over and over. Before I go writing a helper method for personal laziness, is there a shorter or less wrong way to write this sort of statement? Dictionary
Randy Hall
  • 7,716
  • 16
  • 73
  • 151
1
vote
4 answers

How to call Dictionary.TryGetValue() where K : Predicate, V : enum

I have Dictionary, SomeEnum>: var dic = new Dictionary, SomeEnum> { { (d) => d < 10, SomeEnum.Foo }, { (d) => d > 90, SomeEnum.Bar } }; I want to call TryGetValue(K, out V) against it like…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1
vote
1 answer

TryGetValue returns Null even though key exists?

I have a few dictionary objects being combed through to get a value but all of them return null even though when I check to see if they contain the key the one that has the key returns true. I can also use a foreach to go through each one to get the…
Brian Crawford
  • 123
  • 1
  • 10
0
votes
2 answers

C# using TryGetValue after a string split

Okay, i've searched EVERYWHERE and I'm truly stuck with this. I'm trying to create a program which will load a CSV file with text words separated by a comma using a streamreader and then add them into a dictionary. Then on the form if a user types…
user1274876
0
votes
0 answers

How to stop the error: COMError: (-2147220991, 'An event was unable to invoke any of the subscribers', (None, None, None, 0, None))

This program can find a browser by its title and close all tabs and prints all the URLs in the proper window. import time import pywinauto from pywinauto import Application #BrowserTitle will be an array with running instances of…
Justin
  • 41
  • 6
0
votes
2 answers

RouteValues.TryGetValue gives "Local variable 'X' might not be initialized before accessing" error even if the result is true?

if (!this._contextAccessor.HttpContext?.Request.RouteValues.TryGetValue("requiredId", out var idRouteParam) ?? true) return Task.CompletedTask; var id = (int)idRouteParam; I figured by the time it got to the cast everything would work…
Mufasatheking
  • 387
  • 2
  • 6
  • 16
0
votes
1 answer

Improve performance of TryGetValue

I am creating an Excel file using Open XML SDK. In this process, I have a scenario like below. I need to add data into a Dictionary if key is not exists. For that I am using below code. var dataLines =…
Bishan
  • 15,211
  • 52
  • 164
  • 258
0
votes
1 answer

Change Default Integer Value Of C++ Dictionary TryGetValue() Method?

Quick question which I fear has a short and disappointing answer but alas I shall ask anyway.. In the C++ Dictionary method TryGetValue() is there any way to change the default value that will be returned for an integer (to -1 instead for example)…
Darius
  • 5,180
  • 5
  • 47
  • 62
0
votes
0 answers

Overriding GetHashCode with randomly missing properties

I have dictionary based on a custom class. I am facing issues with overriding the GetHashCode() while inheriting IEquatable interface. My class object has three properties: Id1 (string) Id2 (string) Id3 (string) I am retrieving data from two…
Sahil Gupta
  • 103
  • 9
0
votes
1 answer

Why is Dictionary.TryGetValue(TKey, TValue) always returning true?

I'm trying to use Dictionary.TryGetValue() to search a dictionary for a username and password hash, if it matches authenticate the user, if not do something else. The problem I'm having is that anytime the hash value does not match what's in the…
0
votes
2 answers

Retrieve a value from an Enum in Java

Below are two classes Deck and Card. these are being used for a blackjack game and therefore i am trying to get the values of the cards. the values are currently stored in an enum, however i cannot seem to work out how to get the values from the…
Pyson
  • 57
  • 7