Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys, I want to ask, how to declare a global variable. For examples, now I have 2 file .cs, form1.cs and aa.cs.

In file aa.cs, I have create a struct like this
C#
public struct pxlYUV{
            public int y;
            public int u;
            public int v;
        }

public static pxlYUV[] dePixelYUV = new pxlYUV[76800];


and in form1.cs, I want to call variable dePixelYUV. How can I get that variable?
I have tried wrote
public static pxlYUV[] dePixelYUV = new pxlYUV[7680];
dePixelYUV = aa.dePixelYUV;

in form1.cs, but it also not working

thanks for your response
Posted

Files do not matter in C#, visibility and scope do; the scope of name spaces and types define the scope; access modifiers define access to the types/members from different scope.

Now, there is no a concept of "global" variables. The whole idea of global is not good. Using static members can be acceptable in simple projects, but they are hard to maintain correctly in big and complex project.

The really robust concept is the singleton pattern, see http://en.wikipedia.org/wiki/Singleton_pattern[^].

A good sample of singleton implementation for C# is shown here: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Member 7955849 27-Jul-11 5:01am    
it is solved, I was simply put this line :
public DCT.pxlYUV[] dePixelYUV = new DCT.pxlYUV[76800];
on my Form1.cs thanks a lot guys
Sergey Alexandrovich Kryukov 27-Jul-11 13:52pm    
OK, but do I have to explain why it's not the best way to do things?
Anyway, thanks for accepting the answer.
Good luck, call again.
--SA
Your static variable dePixelYUV must be declared in a class that you are not showing us. Given that, you should be able to access it with:

classname.dePixelYUV

Where classname is the name of the actual class that dePixelYUV lives in.
 
Share this answer
 
Comments
Member 7955849 26-Jul-11 9:43am    
sorry no write it down before, but I have declare my static variable on both of files.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900