Home c# fails to implicitly convert type "(int, int)" in "int" c #

fails to implicitly convert type “(int, int)” in “int” c #

Author

Date

Category

I started studying C # a couple of days ago and faced with this problem.

Public Static Int [] Score (BOOL CHECK, INT Player, Int PC) {
  int [] score = {(Check == TRUE)? (Player + = 1, Pc - = 1): (Player - = 1, pc + = 1)};
  RETURN SCORE;
}

My IDE gives an error Unable to implicitly convert the type "(int, int)" in "int" .
I also tried this method of solving the problem:

Public Static Int [] Score (BOOL CHECK, INT Player, Int PC) {
  int [] score = (check == TRUE)? (New int [Player + = 1, Pc - = 1]): (New int [Player - = 1, Pc + = 1]);
  RETURN SCORE;
}

But this method also did not want to work (it fails to implicitly convert the type "int [*, *]" in "int []" ).

What to do?


Answer 1, Authority 100%

explore the difference between the cortes>(a, b) and different types of arrays (type [] – one-dimensional, type [,] – multidimensional and Type [] [] – stepped). In your case one-dimensional array, which is created as follows:

int [] score = (check == true)?
  New int [] {Player + = 1, PC - = 1}:
  New int [] {Player - = 1, PC + = 1};

Answer 2

int [] – a one-dimensional array, you are trying to write a motorcade to it (int player, int pc)
So it is impossible, these are different types.

I think that in your case the following option is suitable:

Public Static (Int Player, Int PC) Score (BOOL CHECK, INT PLAYER, INT PC) {
  (int player, int pc) Score = Check == True? (Player + = 1, Pc - = 1): (Player - = 1, PC + = 1);
  RETURN SCORE;
}
(int Player, int pc) a = score (True, 2, 2); // & LT; - Call it.

or a more advanced option, where there is no answer, and there is just a change in the value:

Public Static Void Score (BOOL CHECK, REF INT PLAYER, REF INT PC)
{// & lt; - Ref gets a link to the value, and not a copy of it, and changes it there, where the function calls
  if (check == true) {Player + = 1; PC - = 1; } else {Player - = 1; pc + = 1; };
}
///
int player = 2;
int pc = 2;
Score (True, Ref Player, Ref PC);

In this case, the Score method will not return anything, but will change the values ​​of Player and PC directly in the place where the function was called (Ref when the transmission should be specified)

If you need to return and not a tuple, I advise you to deal with structures, maybe this is what you need.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions