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 › Check if a parameter has ben provided
Page Index Toggle Pages: 1
Check if a parameter has ben provided (Read 314 times)
Check if a parameter has ben provided
Jul 3rd, 2009, 8:32am
 
Hi all,

I have a class that takes 1 parameter, but I want to make that parameter optional, so if its been provided do something if not do something else, the parameter is a PImage how can I do that?

Thanks
rS
Re: Check if a parameter has ben provided
Reply #1 - Jul 3rd, 2009, 8:47am
 
java doesn't do optional parameters the way php or c++ does

but you can use polymorphism to emulate it

Code:

// this method takes no parameter and passes null to the 'real' method
public void method() {
 method(null);
}
// this method takes one parameter which can be null
public void method(PImage parameter) {
 if (parameter == null) {
   // no parameter supplied
 } else {
   // we have parameter
 }
}


methods have the same name and can take 0 or 1 parameters
Page Index Toggle Pages: 1