如何在所有文件中使用变量 [英] how to use a variable in all file

查看:84
本文介绍了如何在所有文件中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想问一下如何声明全局变量.例如,现在我有2个文件.cs,form1.cs和aa.cs.

在文件aa.cs中,我创建了一个像这样的结构

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

public struct pxlYUV{
            public int y;
            public int u;
            public int v;
        }

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



在form1.cs中,我想调用变量dePixelYUV.如何获取该变量?
我试过写



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;


在form1.cs中,但它也不起作用

感谢您的答复.


in form1.cs, but it also not working

thanks for your response

推荐答案

文件在C#中无关紧要,可见性和作用域都可以.命名空间和类型的范围定义了范围;访问修饰符定义从不同范围访问类型/成员的权限.

现在,没有全局"变量的概念.全球性的整体想法不是很好.在简单项目中使用静态成员是可以接受的,但是在大型复杂项目中很难正确维护它们.

真正强大的概念是单例模式,请参见 http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ].

此处显示了一个很好的C#单例实现示例: http://csharpindepth.com/Articles/General/Singleton.aspx [ ^ ].

—SA
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


您的静态变量dePixelYUV必须在您不向我们展示的类中声明.鉴于此,您应该可以通过以下方式访问它:

classname.dePixelYUV

其中classname是dePixelYUV所在的实际类的名称.
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.


这篇关于如何在所有文件中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆