We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Function in Function
Page Index Toggle Pages: 1
Function in Function (Read 962 times)
Function in Function
Dec 29th, 2009, 11:14am
 
Hej everybody,
I'm having a problem with returning different types. Is it possible to have a kind of subfunction in a function?

My Class CData sorts an array of objects containing country informations. Let's assume I sorted all objects according to the iso country name. On the one hand I want to return a String containing the first iso country name, on the other hand the whole object.

The result would be like this:

Code:
CData.FirstIsoName_String();
CData.FirstIsoName_Object();
...


All in all I'm producing a bunch of functions (There a lot more informations to return...).

Is there any way to return different values in one function?

A little bit like...

Code:
CData.FirstIsoName().returnString();
CData.FirstIsoName().returnObject();


Thanks, Fejngold
Re: Function in Function
Reply #1 - Dec 29th, 2009, 11:51am
 
I am not sure I get your question.

First, let's eliminate one question: no, in Java, you cannot have a sub-function defined in a function.

Now, you can have sub-objects.
Somehow, that's what you describe: your CData has an array of sub-objects.
So, I would rename your function, from FirstIsoName() to FirstIsoCountry(), returning the corresponding object. This object (say, of Country class) can have methods returning the country name, the country code, etc.

So, let say you have cdata, an object of type CData.
You can call: cdata.sort() and then cdata.getFirstIsoCountry() to get the whole first object, and cdata.getFirstIsoCountry().getName(); to get its name (or by using an intermediary variable).

I hope I answered your question.
Re: Function in Function
Reply #2 - Dec 30th, 2009, 4:35am
 
Thanks PhiLho, this works fine.
I now return the object and accessing the country name (or whatever) with the objects method. I'm still wondering why I didn't tested this before.
Page Index Toggle Pages: 1