Jump to content
  • Advertisement
Sign in to follow this  
l jsym l

Question!

This topic is 3303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!