c# - Why can't I call ToString() on an interface? -
i've had different issues re-creating in sample console app, interested know what's going on.
the original problem in code, have class called icat
, class written in c#
public interface icat { string tostring(catcolour colour); }
in same assembly, in c#, there implementation:
public class magiccat : icat { public string tostring(catcolour colour) { return $"i {colour} cat"; } }
this compiles without problems.
in assembly, written in vb.net, have code:
dim mycat icat = getcat() dim result = mycat.tostring() ' error on line
this gives compiler error saying argument not specified parameter 'colour' of 'function tostring(format addressformat) string'.
i tried recreate in c# app, code:
public class cat : ianimal { public string tostring(catcolour colour) { return $"i {colour} cat."; } //public string tostring() //{ // return "i 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 , runs, , output is:
consoleapplication1.cat
red cat.
consoleapplication1.cat
blue cat.
(if uncomment other tostring() method, first line instead > cat.
)
which expect.
i converted application vb.net, expecting original error above, instead got issue:
public class cat implements ianimal public function tostring(colour string) string return "i {colour} cat." end function end class public interface ianimal function tostring(colour string) string end interface
class 'cat' must implement 'function tostring(colour string) string' interface 'ianimal'
so what's happening here? why vb.net giving me error interface implementation, , why original scenario complaining no tostring() method takes no parameters?
edit: have updated vb code this:
public interface ianimal function tostring(colour string) string end interface public class cat implements ianimal public function tostring(colour string) string implements ianimal.tostring return "i {colour} cat." end function end class sub main() dim cat cat = new cat() dim icat ianimal = new cat() call cat.tostring() end sub
i argument not specified parameter 'colour' of 'public function tostring(colour string) string'
original problem, not occur in c# code. idea why? cat object , has blank tostring()
method on it.
you have add bit more information :
public class cat implements ianimal public overloads function tostring(colour string) string implements ianimal.tostring return $"i {colour} cat." end function end class public interface ianimal function tostring(colour string) string end interface
or
public class cat implements ianimal public function mytostring(colour string) string implements ianimal.tostring return $"i {colour} cat." end function end class
Comments
Post a Comment