C
C#2y ago
Czkafek

How to return multiple variables?

I want to return variable win, loss and draw but i dont know how.
22 Replies
Czkafek
CzkafekOP2y ago
My code:
Buddy
Buddy2y ago
$paste
MODiX
MODiX2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Czkafek
CzkafekOP2y ago
em okay
namespace Papier_kamien_nozyce
{
internal class Program
{
static void Main(string[] args)
{
string komputer = " ", user;
int win = 0, loss = 0, draw = 0;
var rand = new Random();
switch (rand.Next(2))
{
case 0:
komputer = "N"; break;
case 1:
komputer = "K"; break;
case 2:
komputer = "P"; break;
}
Console.WriteLine("P - papier, N - nożyce, K - Kamień");
user = Console.ReadLine();
if (user == "p")
{
Papier(user, komputer, win, loss, draw);
}

}

static int Papier(string user, string komputer, int win, int loss, int draw)
{
if (komputer == "N")
{
Console.WriteLine("Przegrałeś! Wyniki:");
Console.WriteLine();
}
return win;
return loss;
return draw;
}
}
}
namespace Papier_kamien_nozyce
{
internal class Program
{
static void Main(string[] args)
{
string komputer = " ", user;
int win = 0, loss = 0, draw = 0;
var rand = new Random();
switch (rand.Next(2))
{
case 0:
komputer = "N"; break;
case 1:
komputer = "K"; break;
case 2:
komputer = "P"; break;
}
Console.WriteLine("P - papier, N - nożyce, K - Kamień");
user = Console.ReadLine();
if (user == "p")
{
Papier(user, komputer, win, loss, draw);
}

}

static int Papier(string user, string komputer, int win, int loss, int draw)
{
if (komputer == "N")
{
Console.WriteLine("Przegrałeś! Wyniki:");
Console.WriteLine();
}
return win;
return loss;
return draw;
}
}
}
its not too long it has some error
Buddy
Buddy2y ago
You can return a tuple if you don't want to create a new class
static (int, int, int) Foo()
{
return (1, 2, 3);
}
static (int, int, int) Foo()
{
return (1, 2, 3);
}
But not sure why you'd like to return the same value as the parameters
Czkafek
CzkafekOP2y ago
How can I use tuples? em
Thinker
Thinker2y ago
static (int, int, int) Foo()
{
return (1, 2, 3);
}
static (int, int, int) Foo()
{
return (1, 2, 3);
}
var (a, b, c) = Foo();
var (a, b, c) = Foo();
So in your case,
static (int, int, int) Papier(string user, string komputer, int win, int loss, int draw)
{
if (komputer == "N")
{
Console.WriteLine("Przegrałeś! Wyniki:");
Console.WriteLine();
}
return (win, loss, draw);
}
static (int, int, int) Papier(string user, string komputer, int win, int loss, int draw)
{
if (komputer == "N")
{
Console.WriteLine("Przegrałeś! Wyniki:");
Console.WriteLine();
}
return (win, loss, draw);
}
Czkafek
CzkafekOP2y ago
I see thanks
Thinker
Thinker2y ago
You could also use a class or a record, but a tuple is probably the easiest here.
Czkafek
CzkafekOP2y ago
thats record?
Thinker
Thinker2y ago
A tuple is not a record
Czkafek
CzkafekOP2y ago
whats*
Thinker
Thinker2y ago
A record would look like this
record PapierResult(int Win, int Loss, int Draw);

static PapierResult(/* ... */)
{
// ...
return new PapierResult(win, loss, draw);
}
record PapierResult(int Win, int Loss, int Draw);

static PapierResult(/* ... */)
{
// ...
return new PapierResult(win, loss, draw);
}
Czkafek
CzkafekOP2y ago
thinking one writing another hmm okay thanks
Czkafek
CzkafekOP2y ago
Buddy
Buddy2y ago
Translate error?
Czkafek
CzkafekOP2y ago
for a non-satic field, method, or propert ... an object reference is required
TheRanger
TheRanger2y ago
well like it says, an object reference is required to access this method it was static in ur code, did u remove that word?
Czkafek
CzkafekOP2y ago
nope
Czkafek
CzkafekOP2y ago
Angius
Angius2y ago
You gave it two return types int (int, int, int) Does it return an int? Or an (int, int, int)?
Czkafek
CzkafekOP2y ago
AAAAAA... yes now everything works
Want results from more Discord servers?
Add your server