C
C#3y ago
Czkafek

How to return multiple variables?

I want to return variable win, loss and draw but i dont know how.
22 Replies
Czkafek
CzkafekOP3y ago
My code:
Buddy
Buddy3y ago
$paste
MODiX
MODiX3y 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
CzkafekOP3y 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
Buddy3y 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
CzkafekOP3y ago
How can I use tuples? em
Thinker
Thinker3y 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
CzkafekOP3y ago
I see thanks
Thinker
Thinker3y ago
You could also use a class or a record, but a tuple is probably the easiest here.
Czkafek
CzkafekOP3y ago
thats record?
Thinker
Thinker3y ago
A tuple is not a record
Czkafek
CzkafekOP3y ago
whats*
Thinker
Thinker3y 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
CzkafekOP3y ago
thinking one writing another hmm okay thanks
Czkafek
CzkafekOP3y ago
Buddy
Buddy3y ago
Translate error?
Czkafek
CzkafekOP3y ago
for a non-satic field, method, or propert ... an object reference is required
TheRanger
TheRanger3y 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
CzkafekOP3y ago
nope
Czkafek
CzkafekOP3y ago
Angius
Angius3y ago
You gave it two return types int (int, int, int) Does it return an int? Or an (int, int, int)?
Czkafek
CzkafekOP3y ago
AAAAAA... yes now everything works

Did you find this page helpful?