0

Can I set a ValueTuple field value only by have its name as string ?

I guess I can do this using Reflection but is there more direct way to do it ?

I want to do something like this :

(string email, int age) user = ("abcd@domain.com", 20)
        
user.SetValue("email", "1234@domain.com");
    
// or
    
user["email"] = "1234@domain.com"
AnGG
  • 679
  • 3
  • 9
  • You may use [an indexer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.ituple.item?view=netcore-3.1#System_Runtime_CompilerServices_ITuple_Item_System_Int32_) for that, but it'll accept an index, not the field name – Pavel Anikhouski Nov 09 '20 at 11:46
  • 1
    why use a valuetuple in the first place when you need access to its members at multipple places? Just create a named class. – MakePeaceGreatAgain Nov 09 '20 at 11:48
  • @PavelAnikhouski i need the name, i do kind of mapping, also i dont see an integer indexer for ValueTuple – AnGG Nov 09 '20 at 11:49
  • @HimBromBeere I don't want to create a class for each one type that i need in this case – AnGG Nov 09 '20 at 11:50
  • 1
    when you have the member-name only at **runtime** rather then **compile**-time, reflection is your way to go. Alternativly a `Dictionary`, but that has the same flaws. – MakePeaceGreatAgain Nov 09 '20 at 11:52
  • From [value tuple field names](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples#tuple-field-names) _At compile time, the compiler replaces non-default field names with the corresponding default names. As a result, explicitly specified or inferred field names aren't available at run time._ The default names in your case is `Item1` and `Item2` – Pavel Anikhouski Nov 09 '20 at 11:55
  • All `ValueTuple`s are the same at runtime, so separate classes are unavoidable if you want to access members by name. You can make those classes anonymous, though (e.g. `new { email = "abcd@domain.com", age = 20 }`), but then of course they are 1) immutable and 2) can't easily straddle scopes. – Jeroen Mostert Nov 09 '20 at 11:55
  • @JeroenMostert anonymous is not a good idea for me because i need to return it from function – AnGG Nov 09 '20 at 11:59
  • @AnGG _"also i dont see an integer indexer for ValueTuple"_ Is accessing them by index good enough for you? – 41686d6564 stands w. Palestine Nov 09 '20 at 12:03
  • Sounds like you need to break out the refactoring tools and start generating those classes/structs, then. Accessing `ValueTuple`s by index or somehow recording these with a mapping won't do anything positive for the readability of your code. C# 9's `record` types will make such classes less painful to spell out. – Jeroen Mostert Nov 09 '20 at 12:06
  • @41686d6564 no, I need to create some kind of map – AnGG Nov 09 '20 at 12:13
  • I think it is already direct, you can simply set it like this: `user.email = "somenewemail@domain.com";` – Jamshaid K. Nov 09 '20 at 12:14
  • @JeroenMostert maybe that is a good idea to use Record, i need to read about this, I haven't yet use this class, maybe its generally what was missing – AnGG Nov 09 '20 at 12:14
  • @JamshaidKamran, I want to do this dynamically with field name – AnGG Nov 09 '20 at 12:16
  • @AnGG Well, as others have said, your custom field names (i.e., `email` and `age`) are converted to `Item1` and `Item2` at runtime ([proof](https://sharplab.io/#v2:EYLgHgbALAPgAgJgIwFgBQcDMACR2DC2A3utmbjnFNgLIAUAlMaea3XEgAzYCmAtgEMAlgBsANNiEA7AC7YBAcx5MArgGceAJ2wBebHQBEA4AGMAJnDhmA9oOkA6E7YMSEnBgG4WrMhwCcdOpa9vzCIp7e2AC+6FFAA=)). So, what you're trying to do is basically impossible with ValueTuples (unless you want to access the values using `"Item1"` and `"Item2"`, which is basically a more complicated way to access them by index). – 41686d6564 stands w. Palestine Nov 09 '20 at 12:19
  • There is no direct way of doing it like that you want. But, you can assign the values to the tuple like this `user = ("new value", user.age);` – Jamshaid K. Nov 09 '20 at 12:20
  • @41686d6564 I understand, yes this is probably impossible in this way, Are Tuples behaving the same ? – AnGG Nov 09 '20 at 12:32
  • @AnGG The `Tuple` class doesn't even provide the syntax sugar that allows you to use custom field names. In other words, you have to use the `tuple.ItemX` properties. Actually, that was the main reason why `ValueTuple` was added to the language. See [this answer](https://stackoverflow.com/a/41084470/8967612) for a detailed comparison, if you're interested. – 41686d6564 stands w. Palestine Nov 09 '20 at 12:51

0 Answers0