locked
How to global a variable in C#? RRS feed

  • Question

  • Hello!

    How can I "global" a variable in C#?


    Thank you!
    Wednesday, September 7, 2005 9:09 PM

Answers

  • There is no such thing as a global variable. However static fields may be what you are after:


    public class MyClass
    {
       public static int MyInt = 10;
    }

     


    Then you can do this:


    MessageBox.Show(MyClass.MyInt.ToString());
     

    Wednesday, September 7, 2005 9:25 PM
  • To declare a method, do the following:


    public static MyClass
    {
       public static void HelloWorld()
       {
          MessageBox.Show("Hello World!");
       }
    }

     
    Wednesday, September 7, 2005 10:47 PM
  • the checkbox is in another "private void" thingy

    I assume you mean the check box event is private.  It returns void.  Your checkbox cannot be void.

    it can't access another's private void'S variables 

    No function can access the variables that exist solely in the scope of another function.

    I take it the check box is on a different form to the text box.  If not, then the code you posted just needs to go into the 'private void thingy' which is the event for the checkbox check.  If it is, then you need to create a delegate.

    http://www.akadia.com/services/dotnet_delegates_and_events.html is one good link on delegates, you can google for more.  Basically, the form with the check box defines an event and an instance of that event, which is public.  When the check box is checked, the event is called.  The form which creates this form is responsible for making sure that this event is tied to a method in the form with the text box, so that it gets called at the right time.
    Thursday, September 8, 2005 1:28 AM

All replies

  • There is no such thing as a global variable. However static fields may be what you are after:


    public class MyClass
    {
       public static int MyInt = 10;
    }

     


    Then you can do this:


    MessageBox.Show(MyClass.MyInt.ToString());
     

    Wednesday, September 7, 2005 9:25 PM
  • Thank you David, I didn't know that I had to specify it but I need to have a "global" variable that need to be processed ; the code you gave me is a class and doesn't allow "regular code" in it.

    I tried creating a new function in it but it doesn't work, it may be because I don't know how to (I've searched and found nothing!).

    I am new to C# (being coding PHP for 2 years though), thank you for your patience :)
    Wednesday, September 7, 2005 10:26 PM
  • To declare a method, do the following:


    public static MyClass
    {
       public static void HelloWorld()
       {
          MessageBox.Show("Hello World!");
       }
    }

     
    Wednesday, September 7, 2005 10:47 PM
  • Hello David, I have tried what you wrote and it gave me a few errors.

    Would you mind giving me a snipplet with this code inside the method?

    Also, is a method the equivalent of a function found in many other languages?


    Thank you!



       if (txtCelsius.Text != "")
       {
        try
        {
         double celsius = double.Parse(txtCelsius.Text);
         double fahrenheitResult = celsius * 1.8 + 32;
         public static string fahrenheitShort = fahrenheitResult.ToString("F1");
         public static string fahrenheitLong = fahrenheitResult.ToString();
        }
        catch
        {
         MessageBox.Show("Erreur: mesure en degrés Celsius invalide!");
        }
       }
       if (txtFahrenheit.Text != "")
       {
        try
        {
         double fahrenheit = double.Parse(txtFahrenheit.Text);
         double celsiusResult = (fahrenheit - 32) * 0.555555555555555;
         public static string celsiusShort = celsiusResult.ToString("F1");
         public static string celsiusLong = celsiusResult.ToString();
        }
        catch
        {
         MessageBox.Show("Erreur: mesure en degrés Fahrenheit invalide!");
        }
       }

     
    Wednesday, September 7, 2005 11:37 PM
  • A method is a function, yes.

    Your code is trying to access a variable that's local to another class.  I don't see any reason for this code to be made global.  The reason C# does not support global variables is because they are not a good idea.  You should create classes for global support only when something must be visible to many classes through the system.  This method does not appear to be a candidate for that.  If it was, you'd need to do these things

    1.  Pass the text from txtCelsius and txtFahrenheit into the method as parameters

    2.  Put your static string declarations outside the method, you can't define variables inside a method to have scope outside it.

    Thursday, September 8, 2005 12:21 AM
  • Hello,

    what I want to do is automaticly refresh the text in textboxes when the user clicks on a specific checkbox. The values to show will already be saved in a variable because the user must have clicked on the "Convert" button at least once before making the checkbox update the textboxes without having to click another time on the "Convert" button. Otherwise, the checkbox wouldn't do anything until the user clicks on the "Convert" button.


    Thank you.
    Thursday, September 8, 2005 12:53 AM
  • So you need to store the strings inside the class and handle the checked event.  There's no reason to use globals to do this.  If the check box is in a different form, use a delegate to pass on the event.
    Thursday, September 8, 2005 12:58 AM
  • Hello Christian,

    the checkbox is in another "private void" thingy and is in the same form, it can't access another's private void'S variables (ie: the one for the Convert button), so I can't fetch the "short" and "long" variables.

    How can I do this?


    Thank you!

    Thursday, September 8, 2005 1:10 AM
  • the checkbox is in another "private void" thingy

    I assume you mean the check box event is private.  It returns void.  Your checkbox cannot be void.

    it can't access another's private void'S variables 

    No function can access the variables that exist solely in the scope of another function.

    I take it the check box is on a different form to the text box.  If not, then the code you posted just needs to go into the 'private void thingy' which is the event for the checkbox check.  If it is, then you need to create a delegate.

    http://www.akadia.com/services/dotnet_delegates_and_events.html is one good link on delegates, you can google for more.  Basically, the form with the check box defines an event and an instance of that event, which is public.  When the check box is checked, the event is called.  The form which creates this form is responsible for making sure that this event is tied to a method in the form with the text box, so that it gets called at the right time.
    Thursday, September 8, 2005 1:28 AM
  • Thank you Christian!
    Thursday, September 8, 2005 1:42 AM
  • Hey! What if I have a different class and in that class I have a method that changes the value of the variable and I want to display that variable data into the main form?
    Tuesday, August 15, 2017 2:25 AM