C
C#

help

✅ Tuples in classes

TTony2/10/2023
using System.Collections.Specialized;

class Program
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' ');
Player player = new Player(input[0], Int32.Parse(input[1]));
}
class Player
{
public string _playerName;
public int playerID;
public Player(string playerName,int playerIdentifier)
{
_playerName = playerName;
playerID = playerIdentifier;
Tuple<string, int> playerInfo = new Tuple<string, int>(_playerName,playerID);
}
}
}
using System.Collections.Specialized;

class Program
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(' ');
Player player = new Player(input[0], Int32.Parse(input[1]));
}
class Player
{
public string _playerName;
public int playerID;
public Player(string playerName,int playerIdentifier)
{
_playerName = playerName;
playerID = playerIdentifier;
Tuple<string, int> playerInfo = new Tuple<string, int>(_playerName,playerID);
}
}
}
This may be a beginner question but how do i acces the playerInfo for each Player instance created? how can i put that tuple into a list so whenever i need a player name and his id i can get it from that list
PPlayboi172/10/2023
There’s a lot of better options here than using a tuple. What would you be using to access the player? It’s name, id, what?
TTony2/10/2023
is not for a game is just for me learning c# and yes his name and his id i want both of them stored somewhere and when i check all the instances of Player Class i can see all of them grouped name with id
PPlayboi172/10/2023
All you need is a List<Player> Players. Then do Players.FirstOrDefault(player => player.id == id) I’m on mobile
TTony2/10/2023
Uh i don't think i know what that is and how to use it
PPlayboi172/10/2023
It’s C#s query language It’s basically saying return the first player in this list where the players id matches my provided id If no player in the list matches the id, return default (null)
TTony2/10/2023
so when i do List<Player> Players; and i iterate trough all of them i will get all the instances?
PPlayboi172/10/2023
Yes
TTony2/10/2023
Damn c# is nice
PPlayboi172/10/2023
If you put all the players in the list It is very nice
TTony2/10/2023
thanks man
PPlayboi172/10/2023
No problem
TThinker2/10/2023
Btw, you should always use (string, int) over Tuple<string, int>
TTony2/10/2023
uh but if i want to iterate over them with a foreach() how do i use that like with a foreach(string player in Players) or how
TThinker2/10/2023
(string, int) is a shorthand for ValueTuple<string, int>, which is a better and more lightweight option to Tuple<string, int>
TTony2/10/2023
thanks i will keep that in mind
TThinker2/10/2023
Iterate over a tuple?
TTony2/10/2023
no
class Player
{
public string _playerName;
public int playerID;
public Player(string playerName,int playerIdentifier)
{
_playerName = playerName;
playerID = playerIdentifier;
List<Player> Players;
}
}
class Player
{
public string _playerName;
public int playerID;
public Player(string playerName,int playerIdentifier)
{
_playerName = playerName;
playerID = playerIdentifier;
List<Player> Players;
}
}
how do i iterate over Players
TThinker2/10/2023
foreach (Player player in Players) { ... }
TTony2/10/2023
oh ok yeah i was using foreach (Player in Players) { ... }
TThinker2/10/2023
you have to specify the type, yeah
AAccord2/11/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
✅ Check a param againstt a stringI'm creating a sort of Animal Dictionary and I need help with check if the name param in the AddToSe✅ ✅ Use function that has default implementation in interface from instance of classI have interface similar to this: ```cs interface IFoo { void DoThing1(); void DoThing2(); vo❔ process.start() works in debug not in iisHello every one. I have an application on iis. The application should run an exe with process start(❔ `delegate*` wrapper throws `System.BadImageFormatException`I wrote some wrapper for `delegate* unmanaged` to use it in F# since it doesn't support function poi✅ Does azure function really cheaper than app service when trigger http ?Hi I understand that the way azure function charge is based on requests not like appservice 24/24 b❔ Any good persistent data solution for .net core 6 c#?Back in .NET framework I use settings and the ``Properties.Settings.Default`` namespace to store dat✅ WPF(asking again) Can I refer cs file in xaml.cs? if so, how❔ Can't use ToastContentBuilder.Show();Hey, so I've created a WPF project in .NET 7.0 and wanted to use the Microsoft.Toolkit.Uwp.Notificat❔ ✅ Help Passing Arguments to Action (Custom Wait Code)Hi, I'm trying to code a custom wait code. This code works, but I want to use methods with arguments❔ WPF Binding TreeViewItem name to string, and falling back to another data context propertyI made a little file explorer app and I wanted to try and allow folders to have no names; The data c❔ Help in sql client query UpdateIn the image as you see thats my datagridview and when i click on the button it will change all Acoe❔ Trying to return the result of Fan Out Fan In Azure FunctionSo I've been trying to build my Azure function with a Fan-Out Fan-In method. But when the function c❔ What is this Blazor Wasm with Server template?What is this Blazor Wasm with Server template?❔ how do i make "dotnet new console" workpls help❔ How can i make this work on windows formsim trying to make this work on windows forms but idk what im doing wrong❔ using data received from a Post to determine what property to setBelow will be attached text of two classes, one called PostReceiveStruct and the other UserAccount. ❔ Saving Images to the database on azureFor my web app i need to save images. my app is hosted on azure back end is asp.net core. when an im❔ broken c# (its a large code explained)```css using System.Runtime.InteropServices; using System.Text; public class Program { [DllImpo✅ Unit tests not appearingHello! For once I'm doing a little project in the console rather than in Unity, meaning for unit tes❔ replace report viewer net core WinFormsHello people, I need to make a ticket and print it, how can I do it? Report viewer in net core versi