locked
Public Variables in C# RRS feed

  • Question

  • Hi there,

    I am just starting to migrate over to C# from Visual Basic 6.0.

    I am having trouble in declaring Puclic Variables which are to be accessed from any where in the application. The application is a database application and uses several forms and modules.

    In VB6.0 we do it by declaring Public Variables in a Module.Bas in the (General) section and that's it. Each of the public variables are then accessible from any where in the application.

    I would appreciate if one of you gurus can tell me how to do it in Visual C# Express.

    Thanks a lot and regards.

    itguru65

     

    Saturday, February 18, 2006 3:48 AM

Answers

  • Use static properties/fields:



    internal static class Settings
    {
       public static string CompanyName = "Company Pty. Ltd";
    }

     

     

    Saturday, February 18, 2006 6:28 AM

All replies

  • Use static properties/fields:



    internal static class Settings
    {
       public static string CompanyName = "Company Pty. Ltd";
    }

     

     

    Saturday, February 18, 2006 6:28 AM
  • Ok. Thanks.

    But where in the sulution (which Form, Program.cs, or any other module) should i insert this code. Thanks again.

    Saturday, February 18, 2006 3:08 PM
  • This is a class, so you can put this in its own file.

    You should read the MSDN Library about C# classes.

    Saturday, February 18, 2006 5:08 PM
  • itguru65 I too am a vb6 programmer moving over to C#. I think you should probably get reading as suggested. "Global variables" kind of go against true OOP you should be able to do all you need without resorting to them by use of the fundamental principles of class programming and passing variables. Obviously there are loads of good sources out there on the net.
    Monday, February 20, 2006 2:54 PM
  • Thanks for the input. I am actually juggling a few balls to attain a higher IT Position and though it's not very professional, but have to port the current application to C# as soon as possible.

    The Reading as suggested is on-going and have to be multiplexed between preparing for CCNA, Migrating to Windows Server 2003 R2, Migrating to SQL Server 2005, Learning C#, Taking care of my IT department and finally prepare for our new baby due in 4 months!

    Any and all help from you IT Pros out there is and will be highly appreciated. Truely hope that I could do something in return for all the advise and guidance I get from these forums.

    Thanks again guys and keep it up. God Bless.

    Monday, February 20, 2006 3:56 PM