C
C#2y ago
Chris TCC

list that holds 2 values each

I want to have a way to store a dynamic amount of data, where each entry holds a string and an int. What is the best way to handle this? I'm thinking along the lines of a 2d list, or if that's not possible, maybe just 2 lists where I use code to compare the values imbetween them (values stored at the same index)
30 Replies
ero
ero2y ago
record DataWithTwoValues(
string StringValue,
int IntValue);

List<DataWithTwoValues> _listOfData = new();
record DataWithTwoValues(
string StringValue,
int IntValue);

List<DataWithTwoValues> _listOfData = new();
Chris TCC
Chris TCCOP2y ago
record? never seen that before
ero
ero2y ago
use a class if you want doesn't matter just anything that holds data make it a class that represents that data records are classes with equality comparison built in
Chris TCC
Chris TCCOP2y ago
I only got a single class I can use, so making it as a class is not possible unfortunately
ero
ero2y ago
make a new one? don't understand
Chris TCC
Chris TCCOP2y ago
because I'm using C# as a scripting language, so everything has to go into a single class if it doesn't go into that class, the entire script breaks
ero
ero2y ago
bogus
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
List<DataWithTwoValues> listOfData = new()
{
new("Foo", 42),
new("Bar", 123)
};

return listOfData;

record DataWithTwoValues(
string StringValue,
int IntValue);
List<DataWithTwoValues> listOfData = new()
{
new("Foo", 42),
new("Bar", 123)
};

return listOfData;

record DataWithTwoValues(
string StringValue,
int IntValue);
Result: List<DataWithTwoValues>
[
{
"stringValue": "Foo",
"intValue": 42
},
{
"stringValue": "Bar",
"intValue": 123
}
]
[
{
"stringValue": "Foo",
"intValue": 42
},
{
"stringValue": "Bar",
"intValue": 123
}
]
Compile: 574.882ms | Execution: 118.395ms | React with ❌ to remove this embed.
Chris TCC
Chris TCCOP2y ago
can you remove items from the list?
ero
ero2y ago
sure?
Chris TCC
Chris TCCOP2y ago
sorry I'm self taught and a complete noob when it comes to this stuff hell, I just learned about lists a few months back before that I was using arrays for everything
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ero
ero2y ago
also if you learned about lists "a few months back", i'd expect you to know everything there is to know about them like it's not that difficult the method is even called .Remove()
malkav
malkav2y ago
Why not use a dictionary? Wait, the conversation is much longer than I expected 😅 I might have missed a bit of the discussion
Chris TCC
Chris TCCOP2y ago
ah. I heard of those but didn't look into that, so I'm not sure what they are I tend to learn things as I need them so... yeah this will def cause bad programming habits
malkav
malkav2y ago
Dictionaries are basically key-value pairs You can set them to hold a string as key, and your int as value. So you'll have both at all times new Dictionary<string, int>() { {"myKey", 1} } or the other way around new Dictionary<int, string>() { {1, "myValue" } }
Chris TCC
Chris TCCOP2y ago
that might be what I'm looking for
malkav
malkav2y ago
dude I was the same with learning any language. "I'll get to those when I need them" 🤣 I know it can work
Chris TCC
Chris TCCOP2y ago
uh yeah no my code is disgusting I gave many aneurisms on my other programming project my first time working on "dynamic" UI stuff where the user can create more items and data and containers
malkav
malkav2y ago
If you want to get your "values" all you do is call the dictionary: myDict[1] or myDict["myKey"]
Chris TCC
Chris TCCOP2y ago
the back end... that's useful, I'll take a look
malkav
malkav2y ago
I do try to keep conventions and such, but I learn stuff when I need thenm
Chris TCC
Chris TCCOP2y ago
uh...
Chris TCC
Chris TCCOP2y ago
this is the backend a triple layered list actually, maybe I might wanna future-proof this is there any way to store data according to a single key? when I do SourcePawn, there is a way to assign variables to certain players, meaning each player has their own storage that you can access I'd like to do something similar in C# oh, I think I found something online: Dictionary, with the variable being a list, and the key is the "player" - would that work?
ero
ero2y ago
HUH locals!
Chris TCC
Chris TCCOP2y ago
no idea what that is
ero
ero2y ago
yeah you do, you're using multiple in that code
Chris TCC
Chris TCCOP2y ago
say what now
ero
ero2y ago
var slots = saveData.busGroups[currentBusGroupIndex].bus[currentBusIndex].busTimeslot;

slots.Add(new() { /* */ });
var slot = slots[currentTimeslotIndex];

slot.panel.GetComponent/* ... */;
var script = slot.busTimesManagerScript = /* */;

script.busDepartureTimeslotUpdate += /* */;
script.UpdateTimingNumber(/* */);
// ...
var slots = saveData.busGroups[currentBusGroupIndex].bus[currentBusIndex].busTimeslot;

slots.Add(new() { /* */ });
var slot = slots[currentTimeslotIndex];

slot.panel.GetComponent/* ... */;
var script = slot.busTimesManagerScript = /* */;

script.busDepartureTimeslotUpdate += /* */;
script.UpdateTimingNumber(/* */);
// ...
your names are all pretty bad and you don't follow conventions
Chris TCC
Chris TCCOP2y ago
yeah they are I agree but tbh I can't be bothered to rewrite the entire system, it's a unity app it took me two months to finally get it working and it works so... kinda off-topic tho. I think I figured out how to do what I wanted to dictionary with a list as the variable
Want results from more Discord servers?
Add your server