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 › How to include a file with class definitions
Page Index Toggle Pages: 1
How to include a file with class definitions? (Read 945 times)
How to include a file with class definitions?
May 18th, 2009, 2:30pm
 
Hi,

My program has grown too long to read so I want to separate the class definitions and store them in a separate file. I tried to create a new tab and copy class definitions into the new tab. When I hit the run button, processing can't find the class in the other tab. What should I do?  I'm pretty used to the C command #include but it's not in processing, or is it? Thanks.
Re: How to include a file with class definitions?
Reply #1 - May 18th, 2009, 3:10pm
 
The 'almost' equivalent tag to #include is import.

If you add a tab, though, it should be considered a part of the same sketch. Are new *.pde files created in your sketch folder?
Re: How to include a file with class definitions?
Reply #2 - May 18th, 2009, 4:24pm
 
You shouldn't need import...

In Ira Greenberg's book he suggests you name the tab that holds a class the same as the class name.  He doesn't specify that this is a requirement, but I guess it might be - I've just done it out of habit.  IIRC in Actionscript you certainly have to name a file that holds a class the same as the class name...  So maybe the same applies here?  Though IIRC saving a class in a separate file is a requirement in AS but not in Processing - so I must admit it seems unlikely that this is the issue.

The other possibility is a syntax error of some kind - e.g. check that you've got the right number of closing braces on your first tab and on the class definitions.  It's easy to accidentally miss one when you cut and paste things around Wink
Re: How to include a file with class definitions?
Reply #3 - May 18th, 2009, 8:21pm
 
Thank you for your replies. I must have had a bad luck picking names for my files. I picked classes.pde. Once I changed it to something else, not even the name of the class, everything worked.
Re: How to include a file with class definitions?
Reply #4 - May 19th, 2009, 1:59am
 
if you stick with plain processing the tab thing is painless and easy.

for the "bad luck with names" thing, i don't know what's going on in your project, but simply keep in mind the basic java naming conventions (ie: no java assigned names, call your class source file with the class name etc) and everything should work fine.
Re: How to include a file with class definitions?
Reply #5 - May 19th, 2009, 3:20am
 
blindfish wrote on May 18th, 2009, 4:24pm:
You shouldn't need import...

Yes, because by default classes see the other classes in the same folder (unless they are private, of course).

Quote:
name the tab that holds a class the same as the class name. [...] So maybe the same applies here

It is a requirement in Java.
In Processing, if the code is in a .pde file, the name is mostly irrelevant, because the code there will be included in the main class.
If the code is in a .java file, you probably must follow this requirement.

Other culprit I found in latest versions of Processing (wasn't a problem with earlier versions because of different naming conventions): avoid naming your sketch the name of a class you use there, eg. ArrayList if you use them, or PShape if you use them: behind the scene Processing will name the main class with this name, and it will be confused (is that self or the foreign class).
Re: How to include a file with class definitions?
Reply #6 - May 19th, 2009, 4:14am
 
Thanks for the clarification.  Are any of these reserved names documented anywhere?  I guess we could find them in the source, but it might be useful to have a list somewhere on the site.  It looks like the OP came up against one of these...
Re: How to include a file with class definitions?
Reply #7 - May 19th, 2009, 7:17am
 
here it is Smiley
Page Index Toggle Pages: 1