Cannot implicitly convert void to string in abstract classes

Asked

Viewed 315 times

-1

My abstract class code has two errors:

  • Cannot implicitly convert "void" to "string" (Line 5, 6, 11, 12, 17, 18, 23, 24)
using static System.Console;

public abstract class DevilHunter
{
    public string Slogan() => Write("Devil May Cry!"); 
    public virtual string Profissao() => Write("Caçar demônio");
}

public class Dante : DevilHunter
{
    public string Estilos() => Write("Possuo 4 estilos de luta");
    public override string Profissao() => Write("Caço dêmonio em grande eSSStilo");
}

public class Nero : DevilHunter
{
    public string DevilBreaker() => Write("Uso Devil Breaker");
    public override string Profissao() => Write("Caço dêmonios ao som de música eletrônica");
}

public class Nico : Nero
{
    public string Dirigir() => Write("Dirige furgão");
    public override string Profissao() => Write("Armeira responsável por criar Devil Breakers para Nero");
}

public class Imprimir
{
    static int Main(string[] args)
    {
        Dante dante = new Dante();
        Nero nero = new Nero();
        Nico nico = new Nico();

        Write(dante.Profissao());
        Write(nero.Profissao());
        Write(nico.Profissao());
        return 0;
    }
}
  • the main method shall be a static X Main() in static class Program where X is the return type, usually void, but it may be int also

  • @Rovannlinhalis this did not work

  • Just one observation: you don’t have any abstract class, and the doubt is not just about it... without knowing how you made the changes and where you are trying to compile also gets difficult. Being in the visual studio, you should have the Program static class, with the Main static method which is where the program will start

1 answer

3


The structure is wrong for what you are trying to do, if, what you are trying to do is print the class functions in the Console.

You currently have the class structure:

class DevilHunter
{
    public string Slogan() => Write("Devil May Cry!"); 
    public virtual string Profissao() => Write("Caçar demônio");
}

The class DevilHunter has two methods where they must return String, however, the body of both methods call the method Console.Write, and it does not return any value, just write in the Console that text.

If you want the methods to return the texts to use elsewhere (which is ideal), you should use properties in a way that returns the texts properly:

class DevilHunter
{
    public string Slogan => "Devil May Cry!"; 
    public virtual string Profissao => "Caçar demônio";
}

Or if you want them to continue being methods that write on the Console, change from string for void, since nothing comes back:

public void Slogan() => Write("Devil May Cry!"); 

However, the line above will not return anything and cannot be called to assign value to something.

Another mistake within your Main is the call of Write to another Write. You need to specify a valid argument for the method Write, and another Write returns nothing.

Using properties, you can simply pass the property as String and it will work correctly. I also changed the method to WriteLine so that there is a break in line between each profession.

class Program
{
    static int Main(string[] args)
    {
        Dante dante = new Dante();
        Nero nero = new Nero();
        Nico nico = new Nico();

        WriteLine(dante.Profissao); // public virtual string Profissao => "...";
        WriteLine(nero.Profissao);
        WriteLine(nico.Profissao);

        return 0;
    }
}

See working on .NET Fiddle.

The boot method depends on a few things, like the C# version and which compiler you’re using. If you are using a lower version than C# 7.1, this method will not work. You must use the signature static void Main(string[]) and not another, what changes is that you may or may not have arguments.

Signatures allowed to the input method, >= version 7.1, are:

public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }

In the documentation you can better understand about. In language reference also says about these signatures.

  • 1

    You were faster than me...

  • @Augustovasques I had already answered 15 minutes ago, however, the answer was incorrect because it was not in the scope of the question. Not what I had understood.

  • The default is to look for a method Main anywhere, there is no standard class name.

  • @LINQ is right, it is described in the language reference: https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/language-specification/introduction

  • @Cypherpotato Still wrong. Main can be without parameters. https://i.imgur.com/uUouL0d.png. In the documentation you cite it says this: "The Main method can be declared with or without a string[] Parameter that contains command-line Arguments."

  • You’re right, but you’re confused, and you take a tone that’s incorrect. Because parameter and visibility are part of the method signature, then you give to understand a different thing.

  • @LINQ if you want, you can edit to collaborate with a simpler explanation.

  • 1

    I know that, but I wanted to warn you. I won’t edit any post to change what it says.

  • For me the problem has not yet been solved

  • @Carlosa. what hasn’t been solved yet? The answer contains the content you will use to solve your problem. If not solved, I suggest you edit your question with clearer content.

  • @Cypherpotato linhas 5, 6, 11, 12, 17, 18, 23 e 24 are in error of Não é possível converter implicitamente tipo void em string.

  • @Carlosa. did you apply my example or even read the answer? Check out the . NET Fiddle link I put in and the code will run. I explained why this mistake happens in your code and how you can fix it.

  • 1

    @Cypherpotato I’m sorry, now it worked the code is running, I just did erase everything from my IDE and copy your code there.

  • @Cypherpotato because the methods didn’t work?

  • @Carlosa. because the return of his statements were string, however, the body returned directly a function that returns nothing (=> Write()). The methods WriteLine and Write Just write what was passed on the Console, return nothing. When you specify a return type in a method, it must return an object with the same type from which it was specified.

  • If I ever changed string for void, could call their methods and write on the console the texts, however, you could not call these methods as obtaining values later, voids and not functions with specific returns.

  • Now I understand, thank you.

Show 12 more comments

Browser other questions tagged

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