How to return a value of unknown type?

J. Doe

I have a very abstract problem ahead of me.

I have a class that has a public property of type Dictionary<string, object> the job of this dictionary is to hold custom user settings and their values.

There are no constraints on the settings but generally they are similar to:
"Brightness", 100
"Connection, "TCP"

So when I instantiate this class its dictionary property will be filled with the names of the settings and their corresponding values.

The problem is retrieving a value.
Since the values can be ints, strings, etc. they are stored as an object in the dictionary but I need to retrieve them as a concrete type.

While I can use dict["Brightness"].GetType() to find out what the type or use pattern matching I am having trouble figuring out what method return type to use.

I thought that, maybe, I can use a separate "result" property for each type and based on the value's type assign the corresponding value to the correct property, so like if the "Brightness" is 100, then have a method that tests the type of the value of "Brightness" and upon seeing it is int do something like

int resultInt;
resultInt = (int)dict["Brightness"];

But something tells me this is ugly and there is a better way.
Furthermore this is not threadsafe.

Additional information on the program's purpose:
The program's idea is to be able to, basically, store a bunch of settings and their values for different systems. They are always in a key-value relationship, keys are unique and values can be anything. They can be strings, ints, floats, decimal, etc. The purpose of the program is user customisation, in a way.
What I mean by this is - the user enters what settings he wants stored. What he enters becomes the full list of possible settings that the program will work with. He then enters values for these settings for each system or "device" that he has. So, for example, he says "I have a computer and a smartphone". Enters the settings "Brightness" and "CPU" into the program. Now the program knows all systems he configures will have these two settings with possibly different values associated with them.
So then the user can say "I want my smartphone screen brightness to be 80%." so he enters the int "80" as the value of "Brightness" for the entry that will correspond to his smartphone. Similarly he can enter a different value for his home computer.

Bottom line I never know what types will need to be stored.

Nathanael Demacon

If you don't know at compile time what type is in your dictionary, you can use the dynamic tag (reworking the example of Patrick):

public dynamic GetValueOrDefault(string key)
{
    if (this.dictionary.TryGetValue(key, out object o))
    {
        return o;
    }

    return null;
}

Use case:

int val;

val = dict.GetValueOrDefault("Brightness");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to handle a return type Maybe<unknown>

How to instantiate value of unknown type in Go?

Unknown return type in template

Unknown Property in a return type

How to use type predicates to give type to an value of type unknown?

How to type return value as "throw"

How to type the return value in Typescript?

How to return lambda of unknown type from function in .NET 3.5?

How do I return a char array of an unknown value?

Waitpid unknown return type in gdb

Std function with unknown return type

Unknown discriminator value "SqlException", How to ignore discriminator on dynamic type

How to refer to the type of a closure in a generic return value?

How to return a value from a function of type `LPCTSTR`?

how to check return value type in scala

How does Scala define a type of return value?

How to type return value as boolean|typeof param

How to correctly type the return value of factory method

How to force type on return value in a generic class?

How to force type on return value in a generic class?

How to type annotate the return value of a macro?

How to return any type value like -performSelector?

How to return string value depending on type of generic

How to set typescript type for matchPath return value

How to return a value of a type aliased in a class?

How to type to prevent a return value in Typescript

How to check the return value of the type() method in Python?

how return a new type with an update value

HTTP Status 500 - ... IllegalArgumentException: Unknown return value type when I use ResponseStatus annotation and return value in spring controller