Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. global variables in c#- the 'extern' alternative

global variables in c#- the 'extern' alternative

Scheduled Pinned Locked Moved C#
csharp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brian55
    wrote on last edited by
    #1

    I'm a c programmer trying to convert my programs to c#. Many variables are global and require extern to access them. I'm a bit confused on how global variables are used in C#. How do a declare and used then in different dll's Brian

    C C 2 Replies Last reply
    0
    • B brian55

      I'm a c programmer trying to convert my programs to c#. Many variables are global and require extern to access them. I'm a bit confused on how global variables are used in C#. How do a declare and used then in different dll's Brian

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      There are no global variables in C#. C to C# is a huge jump, because C does not have classes and C# cannot work without them. If you must have globals, create a class with public static properties on it.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      B 1 Reply Last reply
      0
      • B brian55

        I'm a c programmer trying to convert my programs to c#. Many variables are global and require extern to access them. I'm a bit confused on how global variables are used in C#. How do a declare and used then in different dll's Brian

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        To add to what Christian said. If you are converting your application to C# and are taking advantage of all that OO goodness then you should be aware that in most cases (the vast majority of cases) the feeling that you need a global is an indication of a poor design.


        Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        C 1 Reply Last reply
        0
        • C Colin Angus Mackay

          To add to what Christian said. If you are converting your application to C# and are taking advantage of all that OO goodness then you should be aware that in most cases (the vast majority of cases) the feeling that you need a global is an indication of a poor design.


          Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          yeah, I was holding back, but that's what I meant by 'if you must have globals'. Delegates are a good alternative, they let you tell just the other classes you need to, that something has changed. If only C# had friend classes, things could be a lot tighter than they are.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          1 Reply Last reply
          0
          • C Christian Graus

            There are no global variables in C#. C to C# is a huge jump, because C does not have classes and C# cannot work without them. If you must have globals, create a class with public static properties on it.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            B Offline
            B Offline
            brian55
            wrote on last edited by
            #5

            How do you suggest I deal with 2-d arrays that must accessed by a number of subroutines (methods in C# - if I read correctly). In some cases the data must be passed to nested methods. global vars seem like the best option. If I read what you are saying correctly. I can create a method that contains a struct which would hold the data,declare it static then refer to it anywhere. Am I right? Brian

            C 1 Reply Last reply
            0
            • B brian55

              How do you suggest I deal with 2-d arrays that must accessed by a number of subroutines (methods in C# - if I read correctly). In some cases the data must be passed to nested methods. global vars seem like the best option. If I read what you are saying correctly. I can create a method that contains a struct which would hold the data,declare it static then refer to it anywhere. Am I right? Brian

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Yo can create a property which IS the array, and access it anywhere. I'd be more inclined to pass the data through the subroutines, but it's a matter of style. Globals are ugly, but they do no harm, in and of themselves, if they are not misused.

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              B 1 Reply Last reply
              0
              • C Christian Graus

                Yo can create a property which IS the array, and access it anywhere. I'd be more inclined to pass the data through the subroutines, but it's a matter of style. Globals are ugly, but they do no harm, in and of themselves, if they are not misused.

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                B Offline
                B Offline
                brian55
                wrote on last edited by
                #7

                Thanks, I think I now know how to handle my code. Since it's already written for globals I'll use the method approach. Once I have it running I'll rewrite it passing the functions. The irony here is that I was a fortran programmer who had to leardn C to write this code, now I am moving on once again....after this I plan to learn event driven programing. Good grief, I might not survive that! I'm in the market for good reference books. I thought for sure someone would come up with a good book that explains(ilustrates through examples) how to move from c to c#. If there is, I can't find it. Brian

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups