Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I call ToString() on an interface?

Tags:

c#

vb.net

I've had different issues re-creating this in a sample console app, so I am very interested to know what's going on.

The original problem is that in my code, I have a class called ICat and this class is written in C#

public interface ICat
{
  string ToString(CatColour colour);
}

In the same assembly, in C#, there is an implementation:

public class MagicCat : ICat
{
        public string ToString(CatColour colour)
        {
            return $"I am a {colour} cat";
        }
}

This compiles without any problems.

In another assembly, written in VB.NET, I have this code:

Dim myCat As ICat = GetCat()
Dim result = myCat.ToString() ' Error on this line

This gives a compiler error saying Argument not specified for parameter 'colour' of 'Function ToString(format As AddressFormat) As String'.

I tried to recreate this in a C# app, with this code:

public class Cat : IAnimal
{
    public string ToString(CatColour colour)
    {
        return $"I am a {colour} cat.";
    }

    //public string ToString()
    //{
    //    return "I am a cat.";
    //}
}

public interface IAnimal
{
    string ToString(CatColour colour);
}

class Program
{
    static void Main(string[] args)
    {
        IAnimal cat = new Cat();

        Console.WriteLine(new Cat().ToString());
        Console.WriteLine(new Cat().ToString(CatColour.Red));

        Console.WriteLine(cat.ToString());
        Console.WriteLine(cat.ToString(CatColour.Blue));

        Console.ReadKey();
    }
}

public enum CatColour
{
    Red = 1,
    Blue = 2
}

It compiles and runs, and the output is:

ConsoleApplication1.Cat
I am a Red cat.
ConsoleApplication1.Cat
I am a Blue cat.

(If I uncomment the other ToString() method, the first line is instead > I am a cat.)

Which is what I would expect.

I converted the application to VB.NET, expecting to get the original error above, but instead I got this issue:

Public Class Cat
    Implements IAnimal
    Public Function ToString(colour As String) As String
        Return "I am a {colour} cat."
    End Function
End Class

Public Interface IAnimal
    Function ToString(colour As String) As String
End Interface

Class 'Cat' must implement 'Function ToString(colour As String) As String' for interface 'IAnimal'

So what's happening here? Why is VB.NET giving me an error with my interface implementation, and why is my original scenario complaining about no ToString() method that takes no parameters?


Edit: I have updated my vb code to this:
Public Interface IAnimal
    Function ToString(colour As String) As String
End Interface

Public Class Cat
    Implements IAnimal
    Public Function ToString(colour As String) As String Implements IAnimal.ToString
        Return "I am a {colour} cat."
    End Function
End Class

Sub Main()

    Dim cat As Cat = New Cat()
    Dim icat As IAnimal = New Cat()

    Call cat.ToString()
End Sub

I get Argument not specified for parameter 'colour' of 'Public Function ToString(colour As String) As String' which is the original problem, that does not occur in the C# code. Any idea why? Cat is an object and thus has a blank ToString() method on it.

like image 733
NibblyPig Avatar asked Jan 28 '16 10:01

NibblyPig


People also ask

Why is the toString method never listed in any interface?

Object 's toString method in an interface, you are introducing an ambiguity, because any class implementing your interface would also inherit from Object one way or the other, so the compiler would need to decide between two implementations (despite the fact that you are trying to tell the compiler through the @ ...

Can we define toString method in interface?

Note that the interface does not define a toString() method.

Why is toString not working?

You need to override toString() in the Node class. Show activity on this post. Your Node class does not override the toString() method and falls back to use the Object. toString() method instead.

Can you call toString?

Overview. Every class in Java is a child of the Object class either directly or indirectly. And since the Object class contains a toString() method, we can call toString() on any instance and get its string representation.


1 Answers

You have to add a bit more information :

Public Class Cat
     Implements IAnimal
     Public OverLoads Function ToString(colour As String) As String Implements IAnimal.ToString
         Return $"I am a {colour} cat."
     End Function
End Class

Public Interface IAnimal
    Function ToString(colour As String) As String
End Interface

Or

Public Class Cat
     Implements IAnimal
     Public Function MyToString(colour As String) As String Implements IAnimal.ToString
         Return $"I am a {colour} cat."
     End Function
End Class
like image 157
tumasgiu Avatar answered Oct 12 '22 19:10

tumasgiu