Sarp Kaya
Sarp Kaya

Reputation: 3784

using a global variable on C#

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?

Upvotes: 0

Views: 2096

Answers (2)

ABH
ABH

Reputation: 3439

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;
          ...
     }
}

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44288

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
         {
         }
       }
}

Upvotes: 5

Related Questions