将项目的所有需要​​的结构放在一个单独的文件中 [英] Placing all needed structures of a project in one separate file

查看:45
本文介绍了将项目的所有需要​​的结构放在一个单独的文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

有一个多形式的程序.在其中某些形式中,应使用结构.不知道该怎么做,我将结构的定义放到所有存在该结构类型变量的形式中.我认为应该有更好的方法将结构的定义放在一种形式或文件中,并支持需要该结构的定义的所有其他形式.如果该建议行之有效,请您让我知道如何执行该建议?

非常感谢

Hello,

There is a multi-form program. In some of these forms, a structure should be used. Not knowing what to do, I put the definition of the structure in all forms in which there was a variable of that structure type. I think there should be a better way of putting the definition of the structure in one form or file and supporting all other forms needing the structure''s definition. If the suggestion sounds be working, Could you please let me know how I could implement it?

Thanks a lot

推荐答案

重新定义每种形式的结构不是一个好主意:如果更改一种形式,则必须记住要更改其余形式.此外,如果需要,它们也不能在表单之间共享实例.

如果程序是单个项目,则通过Solution ExplorerL照常创建一个新类,右键单击项目名称,然后选择"Add..New Class"并为其指定结构名称-这将创建一个名为YourStructure的单独文件.cs
class 定义更改为struct 并填写详细信息.
然后,您项目中的每个类都将能够访问结构定义.
您可能需要使struct internal来限制对项目名称空间的访问.

如果每种形式都在一个单独的项目中,则创建一个名为"Utilities"或与DLL类似的新项目,并在其中包含您的结构.然后,您可以在每个表单项目中添加对实用程序的引用.在这种情况下,您希望将struct定义为public而不是internal
It is not a good idea to re-define your structure in each form: If you change one, you must remember to change the rest. Also, they cannot share instances between forms if you need to.

If the program is a single project, then create a new class as usual via Solution ExplorerL Right click the project name, then select "Add..New Class" and give it the name of your structure - this will create a separate file called YourStructure.cs
Change the class definition to struct and fill in the details.
Each class within your project will then be able to access the structure definition.
You may want to make the struct internal to limit access to the project namespace.

If each form is in a separate project, then create a new project called "Utilities" or similar as a DLL and include your structure in that. You can then add a reference to Utilities in each form project. In this case, you will want the struct to be defined as public rather than internal


.您应该使用该结构的定义创建一个类文件. > 在您的表单中设置using ns.class;对其的引用.
现在您可以定义该类型的变量.

干杯
You should create a class file with the definition of that structure.
In your form set a reference to it by using ns.class;.
Now you can define variables of that type.

Cheers


这篇关于将项目的所有需要​​的结构放在一个单独的文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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