global variables in c#- the 'extern' alternative
-
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
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 )
-
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
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
-
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
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 )
-
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 )
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
-
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
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 )
-
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 )
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