在哪里定义类 [英] Where to define Classes

查看:103
本文介绍了在哪里定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VS&C#的新手,所以请多包涵...

在VB中编程时,我可以设置一个称为定义"的模块,并在其中定义将在整个程序中使用的任何和所有变量.

定义类时,我可以在VS2010 .net C#中做类似的事情吗?

I am new to VS & C# so please bear with me ...

When programming in VB, I can set up a Module called Definitions and within that I can define any and all Variables that I will use throughout the entire Program.

Can I do anything similar in VS2010 .net C# when defining Classes ?

推荐答案

使用静态数据并将其设置为应用程序全局是一件坏事.一个大问题是缺少在不同线程中使用它们的同步方法.您不会想出在没有线程的情况下设计真正高级应用程序的想法,是吗?有一些方法可以避免静态数据-完全或几乎在所有情况下都可以避免.

我建议您仅使用静态类声明一组全局"常量和只读对象,而没有别的.好吧,如果您的应用程序真的很简单,您还可以将这种方法用于其他用途.

除此之外,您真的想变得更认真.因此,您可以更好地使用Singleton 设计模式,请参见:
http://en.wikipedia.org/wiki/Singleton_pattern [ http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 [ ^ ].

您可以在此处找到良好的代码示例: http://csharpindepth.com/Articles/General/Singleton.aspx [ ^ ].

我的其他建议是避免额外单身人士.这是示例:假设您的UI中有一个主窗体或一个主窗口.该对象已经扮演了单例角色,因此您可以将带有声明的应用程序全局类作为其成员,而不是创建单独的单例.单例或静态对象越少越好.

—SA
Using static data and making it application-global is a bad thing. One big problem is the lack of synchronization methods for using them in different threads. You are not going to play with the idea of designing a really advanced applications without threading, are you? There are ways to avoid static data — at all, or in almost all situations.

I would advise you to use a static class only to declare a set of "global" constants and read-only objects, nothing else. Well, you can also use this approach for something else if your application is really simple.

Beyond that, you really want to be more serious. So, you can better use the Singleton design pattern, see:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29[^].

You can find good code samples here: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

My other advice would be to avoid extra singletons. Here is the example: suppose you have a main form or a main window in you UI. This object already plays the role of a singleton, so you can put your application-global class with the declarations as a member of it instead of creating a separate singleton. The less singletons or static objects, the better.

—SA


通常,您不需要全局变量;大多数交互式应用程序只有一个启动类(对于桌面UI应用程序是一种主要形式),而其他形式将是子类(从逻辑上讲就是这种形式).因此,主要形式可以创建它们,挂钩到事件处理程序或公开回调挂钩,并且主要形式(或后面的模型类,如果您做得正确的话)可以是打开内容的存储库,依此类推.

在极少数情况下,这是行不通的,例如,如果您的应用程序没有主要形式"的概念,而是需要显示各种具有相同优先级的相似形式.这是不寻常的,因此我不会详细介绍,但是如果您认为这是您需要的,请在此解决方案下做出答复,以更详细地说明您的问题.

看一下您在解决方案2中发布的内容,我认为对于尝试编写UI应用程序的大型机或DOS程序员来说,这是一个非常典型的错误:您仍然会考虑屏幕"并将一个屏幕转换为一种形式.这不是用户期望与图形OS上的应用程序进行交互的方式,通常,这是UI的错误设计方法.根据应用程序的复杂性,有两种不错的方法(可以通过使用高级..."按钮进行组合):(i)将所有内容都包含在一种形式中,但在选项卡中,每个旧选项卡中包含一个选项卡屏幕(粗略地说),以及(ii)很少使用的选项,显示一个对话框(模态形式)以仅编辑这些值.这两种方法都可以在一个表单类的范围内完成,因为(i)显然所有内容都存在,并且(ii)主表单可以生成模式编辑器并在关闭时处理模型更新.
Generally, you shouldn''t need global variables; most interactive applications have a single startup class (a main form, in the case of a desktop UI application), and other forms will be children (logically speaking of that one). So the main form can create them, hook onto event handlers or expose callback hooks, and the main form (or the model class behind it, if you''re doing things properly) can be the repository for what is open and so on.

There are rare cases where this doesn''t work, for example if your application doesn''t have a concept of ''the main form'' and instead needs to show a variety of similar forms that all have equal precedence. This is unusual so I won''t go into it in detail, but if you think this is what you need, please respond under this solution explaining your problem in a bit more detail.

Looking at what you posted in Solution 2, I think you are making a very typical mistake for mainframe or DOS programmers trying to write UI apps: you still think in terms of ''screens'' and translate one screen to one form. This is not how users will expect to interact with an application on a graphical OS and in general it''s the wrong design approach for the UI. Depending on the complexity of the application, two good approaches (which can be combined through the use of ''Advanced ...'' buttons) are: (i) to include everything on one form, but in tabs, one tab per old screen (roughly speaking), and (ii) for rarely used options, show a dialog box (modal form) to edit just those values. Both of those can be done within the scope of one form class, as in (i) everything is obviously present there, and in (ii) the main form can spawn the modal editors and handle updating the model on close.


您需要在C#中使用以实现类似功能的是具有公共静态属性的静态"类.
What you need to use in C# to achieve a similar functionality is a "static" class with public static properties.


这篇关于在哪里定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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