Question!

Started by
4 comments, last by l jsym l 14 years, 5 months ago
Hey, I was just wondering if their is anyway to create an array within a user-defined method. What i want to do is get some users names and total amounts of votes they got for an election. the problem I am having is that when i go to set the name and the total votes into the arrays through the method, I can't print them out in the main class then? It acts as if the arrays only exist within that method and I dont know how to change that.. I dont really know how to explain it but if anyone understands what i'm getting at it would be very helpful.
l jsym l
Advertisement
You probably can. But we would need to know what language you are using to give you concrete advice. You could post your code too, if its not long enough.
Hey!

Maybe you should paste the code of what you have done so far, and tell us which language you are using. People will understand the problem better if you do.
If his(?) other threads are any indication, Java. And he needs to review the chapter discussing scope...
Scope.

Look at this code (yours, probably):
class Something{   int Main()   {      // array does not exist within this method   }    int UserDefined()   {       Array arrayVisibleOnlyToUserDefined;       // Here you can use array   }}


and this code (working):

class Something{   Array arrayVisibleToAllMethodsWithinSomething;   int Main()   {      // Here you can use array   }    int UserDefined()   {      // Here you can use array   }}


Variables (including Array) are only visible within the code block ( delimited by { and } ) they are declared in. Hope this helps. And improve your grammar (dont -> don't; i -> I).
Lesan you are right. I was trying to instansiate the array in the main when it has to be before it...stupid little mistake on my part.

Thanks guys!

and it was in JAVA
l jsym l

This topic is closed to new replies.

Advertisement