C
C#2y ago
Primpy

❔ How to make a list of tuples that hold references?

I want to create a List that holds reference for two types of objects: one class object and one string. For this, I tried using tuples. However, it seems like when I create a tuple (with a constructor or with Tuple.Create), copies of those two objects will be created instead of references. What would be a solution for this?
15 Replies
ero
ero2y ago
that shouldn't be the case. can you show what you're doing and how you're testing it?
MODiX
MODiX2y ago
just_ero#0000
REPL Result: Success
C c = new();
List<(C, string)> list = new()
{
(c, "foo")
};

list[0].Item1.I = 123;

Console.WriteLine(c.I);

class C
{
public int I { get; set; }
}
C c = new();
List<(C, string)> list = new()
{
(c, "foo")
};

list[0].Item1.I = 123;

Console.WriteLine(c.I);

class C
{
public int I { get; set; }
}
Console Output
123
123
Compile: 691.652ms | Execution: 53.088ms | React with ❌ to remove this embed.
Primpy
PrimpyOP2y ago
It's part of a larger project. The relevant code looks something like this:
public static List<(Text, string)> LocalizedText { get; set; } = new();

public static void SetEnglish()
{
StartGameButtonString = "START GAME";
...
}

public static void SetRomanian()
{
StartGameButtonString = "INCEPE JOCUL";
...
}

public static void AddLocalizedText(Text text, string str) { LocalizedText.Add((text, str)); }

public static void Update()
{
Initialize();
foreach (var localizedText in LocalizedText)
{
localizedText.Item1.DisplayedString = localizedText.Item2;
}
}
public static List<(Text, string)> LocalizedText { get; set; } = new();

public static void SetEnglish()
{
StartGameButtonString = "START GAME";
...
}

public static void SetRomanian()
{
StartGameButtonString = "INCEPE JOCUL";
...
}

public static void AddLocalizedText(Text text, string str) { LocalizedText.Add((text, str)); }

public static void Update()
{
Initialize();
foreach (var localizedText in LocalizedText)
{
localizedText.Item1.DisplayedString = localizedText.Item2;
}
}
I noticed the problem when placing a breakpoint inside the Update() function. Even though the contents of StartGameButtonString were updated, the string inside localizedText.Item2 was not. Is the problem here or should I look somewhere else?
ero
ero2y ago
you don't update Item2 ever.
Primpy
PrimpyOP2y ago
My goal was to have Item2 have a reference to the string so it would automatically update. Is that possible?
ero
ero2y ago
to what string? also it's probably best not trying to roll your own way of localizing your app
Primpy
PrimpyOP2y ago
To clarify, AddLocalizedText is called with the parameter LanguageStrings.StartGameButtonString, which is a string declared in the body of my class. The desired result is for the Item2 of LocalizedText to update by itself whenever the value of StartGameButtonString is modified. Sorry if I worded this poorly.
ero
ero2y ago
that seems more like a major design flaw
Primpy
PrimpyOP2y ago
I'm aware, it's a patchwork solution for a project with a deadline that is pretty close...
ero
ero2y ago
(and is not possible)
Primpy
PrimpyOP2y ago
I'm aware... Either way, unfortunate for me that I can't hold references like that. Thanks for the help, I'll see what I can do about it.
ero
ero2y ago
why not just clear the list and repopulate it? that's basically what you're doing anyway
public static void SetEnglish()
{
LocalizedText.Clear();

AddLocalizedText(nameof(StartGameButtonString), "START GAME");
}
public static void SetEnglish()
{
LocalizedText.Clear();

AddLocalizedText(nameof(StartGameButtonString), "START GAME");
}
Primpy
PrimpyOP2y ago
Oh, I think I could do that. And that usage of "nameof" gave me yet another idea on how I could implement this. Thanks!
Jimmacle
Jimmacle2y ago
strings have value type semantics even though they're reference types, that's why you don't see the changes "propagating" through other references set using the one you changed i guess even then it wouldn't work without some kind of wrapper PepeHmmm
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server