Forums

ToString / toString

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

ToString / toString

Postby neuruss » Fri Feb 08, 2008 5:04 am

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]'
Last edited by neuruss on Fri Feb 08, 2008 6:07 am, edited 1 time in total.
neuruss
 
Posts: 23

Re: ToString / toString

Postby neuruss » Fri Feb 08, 2008 6:05 am

Nevermind, I solved it.
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

Postby Charles » Fri Feb 08, 2008 11:20 am

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.
Charles
 
Posts: 2510
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron