Code Comments
Programming Forum and web based access to our favorite programming groups.pls, what is syntax for methods that return void? for example cookie.setComment()... I can do cookie.setComment(); but how do you assign return to a var? and why does a method like this return void (i.e., nothing, right?) when setComment it would seem to me is a string (same for other methods to set values for cookies..) I mean get an error here.. (since method doesn't return a string..) string comment = cookie.setComment(); so how do you do this? void comment = setComment()?? obviously got an error here too..... thank you... Frances
Post Follow-up to this message"Frances Del Rio" <fdr58@yahoo.com> wrote in message news:42a094e5$1_1@x-privat.org... > > pls, what is syntax for methods that return void? > > for example cookie.setComment()... > > I can do > > cookie.setComment(); > > but how do you assign return to a var? and why does a method like this > return void (i.e., nothing, right?) when setComment it would seem to me is > a string (same for other methods to set values for cookies..) > > I mean get an error here.. (since method doesn't return a string..) > > string comment = cookie.setComment(); > > so how do you do this? > > void comment = setComment()?? > > obviously got an error here too..... thank you... > > Frances If something returns void it means it doesnt actually return anything, therefore it is a syntax error to try and assign nothing to a variable. If you have a look at a method that returns void then you will probably see no 'return' in it, but if you do it will say 'return' and nothing else. For your example, I would imagine setComment() takes a String as a paramater, and the designer has decided to not return anything which makes sense since all you are doing is passing a message in effect. Some programmers never return void, instead they take the opportunity to at least return a boolean for sucess/failure but this is a design choice. Anyway, the short answer is you cannot store nothing in a variable, therefore assigning the result of a method that returns void to a variable doesnt work. Allan
Post Follow-up to this messageOn Fri, 03 Jun 2005 13:35:14 -0400, Frances Del Rio <fdr58@yahoo.com> wrote: > >pls, what is syntax for methods that return void? > >for example cookie.setComment()... > >I can do > > cookie.setComment(); > >but how do you assign return to a var? and why does a method like this >return void (i.e., nothing, right?) when setComment it would seem to me >is a string (same for other methods to set values for cookies..) > >I mean get an error here.. (since method doesn't return a string..) > > string comment = cookie.setComment(); > >so how do you do this? > >void comment = setComment()?? > >obviously got an error here too..... thank you... Not sure what you are asking, but void methods do not return anything, so attempting to assign something a value from that kind of function will not compile. If you need to get the comment from the cookie, I'm willing to bet there's a function getComment().. -- now with more cowbell
Post Follow-up to this messageAllan Bruce wrote: > "Frances Del Rio" <fdr58@yahoo.com> wrote in message > news:42a094e5$1_1@x-privat.org... > > > > If something returns void it means it doesnt actually return anything, > therefore it is a syntax error to try and assign nothing to a variable. I f > you have a look at a method that returns void then you will probably see n o > 'return' in it, but if you do it will say 'return' and nothing else. > For your example, I would imagine setComment() takes a String as a > paramater, and the designer has decided to not return anything which makes > sense since all you are doing is passing a message in effect. Some > programmers never return void, instead they take the opportunity to at lea st > return a boolean for sucess/failure but this is a design choice. > Anyway, the short answer is you cannot store nothing in a variable, > therefore assigning the result of a method that returns void to a variable > doesnt work. ok, many thanks for yr response.. I looked at other "set" methods in Servlet API (specif. in HttpServletResponse & ServletResponse) and well, they all return void.. I still think this is weird.. (but Bryce is right, of course, specif. in my ex., there IS a getComment() method, and probaby all the others have a "get" equiv. also..) again many thanks, also to Bryce.. Frances
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.