0

So I have to use this void

static void PerformOption(int option)

However I want this int option to be able to be used by other voids as well. I know this could be done by deleting inside of brackets and assigning a static int but I have to use int option for this void, so I cannot change this one.

So how can I make option as global variable?

3
  • 9
    your question is void, please try again :) Commented Mar 26, 2012 at 3:18
  • If you want the argument to be used by other methods, then it all depends how your methods are organized. If those methods are called by this method, then keep passing this as arguments to others. If they are called seperately, then make this argument as global/class member. Commented Mar 26, 2012 at 3:19
  • You can create a static int and assign option to that static int inside PerformOption but I am almost certain that there is a better way to implement whatever you're trying to do. Commented Mar 26, 2012 at 3:21

2 Answers 2

5
public class MyGlobalsBecauseImEvilAndLoveBadProgrammingMemes
{
   public static int Spaghetti { get; set; }
}

then in PerformOption do

MyGlobalsBecauseImEvilAndLoveBadProgrammingMemes.Spaghetti = option;

but hopefully you want a member, then do

  public class MyClass
    {
       int Option { get; set; }
       public void PerformOption(int option)
       {
          Option = option
          // other stuff
       }

       public void SomethingElse()
       {
         if(Option == 1)  // use Option at will
         {
         }
       }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Declare a global int variable to hold option value inside the PerformOption method to be used in other methods.

public static class MyClass
{
     private static int globalOption;

     public static void PerformOption(int option)
     {
          globalOption = option;
          ...
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.