This is what I get when I try to override the method toString:
hello.cobra(47): error: Cannot return String because "toString" is declared to return a void.
What's wrong? Isn't it supposed to return a string?
this is my class:
class Dude
shared
var _l = List<of Dude>()
var _name = ''
var _last = ''
var _age as int
def init(name as String, last as String, age as int)
_name = name.toUpper
_last = last.toUpper
_age = age
_l.add(this)
pro name from var
pro last from var
pro age from var
pro l from var
def toString is override
return '[_name] [_last]'
Forums
ToString / toString
3 posts
• Page 1 of 1
ToString / toString
Last edited by neuruss on Fri Feb 08, 2008 6:07 am, edited 1 time in total.
- neuruss
- Posts: 23
Re: ToString / toString
Nevermind, I solved it.
I just had to add "as String" to the method:
def toString as String is override
return '[_name] [_last]'
I just had to add "as String" to the method:
def toString as String is override
return '[_name] [_last]'
- neuruss
- Posts: 23
Re: ToString / toString
If you put your code in (cobra)... (/cobra) tags, but with square brackets instead of parens, then the formatting will be preserved.
So my take away from this is that the error message is poor:
hello.cobra(47): error: Cannot return String because "toString" is declared to return a void.
The word "void" is in the error message but not found in "def toString is override" and there was no warning that you cannot overload by return type.
So my take away from this is that the error message is poor:
hello.cobra(47): error: Cannot return String because "toString" is declared to return a void.
The word "void" is in the error message but not found in "def toString is override" and there was no warning that you cannot overload by return type.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 41 guests