How to fix this error "Cannot Convert from 'int' to 'char[]'

Asked

Viewed 455 times

4

Why is this error presented? I believe the logic is right.

Tela do Erro

  • Put the same code in text and the error description, it is easier to help.

  • Want to print the three values on the console? Use this method Verload Console.WriteLine(String).

2 answers

6


Formatting the output

The first argument of the method WriteLine must be a string.

This string can contain markers to insert into it, the following arguments:

Console.WriteLine("{0} {1} {2}", centena, dezena, unidade);

About the error

The indicated error occurs because the method WriteLine has more than one way of being called. One of these forms receives char[] in the first argument, followed by two ints. What happens is that the compiler is evaluating the possible alternatives, and he thought that was the one you wanted to call.

  • Blza, fixed the problem, meaning error was it because it was not converting the values to string? Because generally, I believe, just put the values in Console.Writeline and he turned to do the cast

  • I tried to explain the error in the answer better.

  • Hi @Miguel Angelo, thanks so much for trying to explain, but I believe I understand the problem. I wanted to overload the Writeline method only with interim numbers, but there is no such possibility, in the method it usually starts with string and then comes the objects according to the documentation.

  • Exactly, none of the alternatives of this method gets 3 integers. I think the compiler tries to see which is the most accurate possibility in terms, in your case, trying to pass 3 integers, he found the alternative with char[], int, int, which has only a different type compared to int, int, int, and he inferred that maybe that’s the one you wanted to call.

  • Flawlessly!!!

4

See the documentation of this method.

There are several overloads to call you, each handles the information in a different way. Most are for basic types of language and as can be seen only accepts one argument.

To overload that accepts multiple arguments cannot be used directly, the first argument must be a string with the formatting that will be applied to the content and then the arguments that should be printed using this formatting. This is what you should use.

There is a another that accepts more than one argument. How this overload accepts int as second and third argument, it is the one that has come closer to what it seems to need, so the compiler mistakenly choose this.

This is called betterness.

In these cases the ideal is to use interpolation of string, avoids a lot of problems and becomes more readable (some disagree). So:

using static System.Console;

public class Program {
    public static void Main() {
        WriteLine("Digite um número com três dígitos");
        if (int.TryParse(ReadLine(), out var numero) && numero < 10000) WriteLine($"{numero / 100} {(numero % 100) / 10} {(numero % 100) % 10}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I fixed other code issues and made it more efficient and simple.

  • True Maniero, now yes I understood the error. The problem that wanted to overload the Writeline method only with integer values. But I looked at the documentation and realized my mistake. Thank you so much for the explanation.

  • If you had overloads for all of this, it would generate a combinatorial explosion, it’s not viable. So you have the main basics and then the general. If you put the text I’ll put together a better code for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.