What does the error "Cannot implicitly convert "void" to "int" refer to?

Asked

Viewed 528 times

1

My goal is to calculate percentage, while trying to run the code below I get the error message:

"Cannot convert type implicitly void in int.

As I correct?

int var1 = Console.WriteLine("Insira bullshit");
int var2 = Console.WriteLine("Insira bullshit");
int Resultado = var1 / 100 * var2;

Console.ReadLine(); 
Console.WriteLine($"Resultado: {Resultado}");

2 answers

2

The method WriteLine() has as return void, ie returns nothing, then has no value in the execution of it, and is trying to store a value in the variable, this is not possible, need to use a method that returns some value to be able to store in the variable, and in the specific case has to be a type int, so I could not indicate the ReadLine() also that returns a string.

But what you want seems to be for someone to type something, the question is not clear on this and without understanding the problem becomes complicated to find a suitable solution, I will guess that it is this. Then you’ll have to use the ReadLine() to take the typed data, convert it to intsafely and then you can use in the desired calculation, luckily this is one of the exercises that people post most here and has a lot of code that you can see how is the right.

Of course you will only learn if you understand what each thing does, so it would be good to do new research on the subject, right here on the site there is a lot, and if you do not have can ask a new more specific questions.

I’m not going to put the code here because it doesn’t make sense to put another example of how to use the ReadLine() in the right way has a huge amount of questions that this has already been demonstrated and would even make the question as duplicate if it went this way:

  • But, the question is a duplicate ... could be closed, is closed here for less than this and even the guy does not care what was answered.

  • If the person does not care for what was answered I can not say that is infer something that I have no control and do not have subsidy for so much. I don’t think it’s duplicate if you choose one and shut it down. And don’t answer questions that you think are duplicate. I answered in the first paragraph what really is the problem asked, then I put other points that if it were the focus would duplicate. Remembering that duplicate is something that already has the answer, and show how to check the data properly when using the ReadLine(), is something already answered and would be duplicate

  • is duplicate yes, stop being that way and you know it’s duplicate that the guy has no kind of knowledge and C# and in programming is not explicit but, yes implicit ... need not attack anyone and I did not attack you, I just put what it really is. If it wasn’t duplicate the links you called in your answer it wouldn’t be the answer to that question.

  • Now you’re trying to instill my thoughts, that’s not cool. I don’t answer based on whether or not the person knows something, answer if the question is asked in a way that can be answered and add something useful there. I do not think duplicate, including the question you marked does not talk about what was question but about the use of TryParse(), that if it were the case it would be duplicate, but clearly the question does not talk about this, it is additional information.

  • Okay Maniero, you’re like that and that’s it.

2

Console.Writeline is to display information on screen in a program Console Application, to pick up values typed on the screen the command is Console.Readline.

Your code can be made like this:

public class Program
{
    public static void Main()
    {
        decimal var1 = 0;
        decimal var2 = 0;
        if (decimal.TryParse(System.Console.ReadLine(), out var1) &&
            decimal.TryParse(System.Console.ReadLine(), out var2))
        {
            decimal Resultado = (var1 / 100) * var2;
            System.Console.WriteLine(var1);
            System.Console.WriteLine(var2);
            System.Console.WriteLine(Resultado);
        }
        else
        {
            System.Console.WriteLine("Error");
        }
    }
}

and when you work with division it’s best to work with data type decimal because a division can return values with commas.

Reference:

  • I did not understand what "out" represents in this code, could elucidate?

  • the out it makes the argument reference Reading. If the TryParse is a success this variable has the test value for you to use in your code @Misaell

Browser other questions tagged

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