正确的方法使用全局常量 [英] The right way to use Globals Constants

查看:149
本文介绍了正确的方法使用全局常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在几乎每一个项目,我不能决定如何处理某些全局常量的值。在较老的日子里,当我写这并没有使用DLL的C ++程序,很容易。只需创建并与多家常量的说明某一恒定值,我的项目的.h文件。然后,我有充分的文件包括它,当当!有效。清洁,尊重DRY原则和简单。

In almost every project, I can't decide on how to deal with certain global constant values. In the older days, when I wrote C++ programs which didn't used dll's, it was easy. Just create and .h file with a number of const that described certain constant values for my project. Then I had every file include it, and ta-da! It worked. Clean, respected the DRY principle and was simple.

现在我的项目是C#.NET,这给了我一个很大的选择范围来处理这个问题。据我所知:

Now my projects are C# .Net, which gives me a large range of options to deal with this problem. From what I know:

  1. 创建大会,其唯一目的就是用于存放常数值为我的项目。每隔大会应然后引用这个。我尊重干燥,KISS,因为添加引用的是很简单的。这里主要的问题是,我需要重新编译来更新这些值的全部源。

  1. Create an Assembly whose only purpose is to hold constant values for my project. Every other Assembly should then reference this one. I respect DRY and KISS, since adding references is simple enough. Main problem here is that I'd need to recompile the whole source to update those values.

使用app.config文件并让所有其他组件检索的不断初始化过程中。所以,我想补充有初始化一切只是访问全球价值的开销。更灵活,也更痛苦。

Use a app.config file and have all other Assemblies retrieve the constant during initialization. So I add the overhead of having to initialize everything just to access a global value. Is more flexible but also more painful.

使用的资源。我认为这是相同的,与app.config中。

Use resources. I think it's the same as with app.config.

所以,我知道有一个更好的方式来做到这一点的常量声明。但我不知道,到目前为止,已经无法找到如何做到这一点。能否请你帮忙吗?我的糖果!

So, I know there's a better way to do this constants declaration. But I don't know and, so far, have been unable to find how to do it. Can you please help? I have candy!

感谢所有

推荐答案

如果您希望值能够在运行时被改变,使用的app.config。

If you want the values to be capable of being changed at runtime, use app.config.

。在C#中的情况下,某种GlobalValues​​类/组件的。

If you want them to be fixed at runtime then you're going to have to (and want to, to stop users messing around with them) recompile every time you want to change them, so use whatever's appropriate for your language. In the case of C#, some kind of GlobalValues class/assembly.

不,除非你想换套和淘汰的价值观作为一组使用全局值或设置一个资源文件(如编译不同语言时)。

Don't use a resource file for global values or settings unless you want to swap sets of values in and out as a group (e.g. when compiling for a different language).

这篇关于正确的方法使用全局常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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