如何在vb中声明一个全局常量列表? [英] How to declare a global list of constants in vb?

查看:90
本文介绍了如何在vb中声明一个全局常量列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想创建一个常量列表(75,90,110,125,140,​​160)。然后我想评估哪个数字最接近但总是高于给定数字。



对于实例,给定121.5,我想创建一个返回的函数125.



我该怎么做?



谢谢

Hi,

I want to create a list of integers (75,90,110,125,140,160) that are constants. I then would like to evaluate which number is closest but always higher than a given number.

FOr instance, given 121.5, I'd like to create a function which returns 125.

How can I do this?

Thanks

推荐答案

.NET中没有全局对象这样的东西。这非常好,因为全局对象可能不安全并且表明编程风格不好。对象可以在一个或另一个上下文中共享。



但是,这不适用于常量,也适用于静态不可变对象(在语法不允许常量的情况下扮演常量的角色,例如,如果你想拥有某个东西的不可变数组,或任何其他只初始化一次且无法修改的对象。



由于常量不能被修改,如果它们在所有上下文中都可访问,则没有人携带。但是,没有什么必须是全球性的。一切都应该有其背景。比如说,可以从进程中的任何程序集访问具有 public 访问修饰符的顶级类型,并且 internal (默认值对于顶级类型)将使您的程序集中的顶级类型可访问。



您没有指定要使用的范围,所以让我们假设这是整个组件。



现在,让我们定义常数整数。它实际上取决于目的。这是最紧凑的方式:

There is no such thing as global object in .NET. And this is very good, because global objects are potentially unsafe and indicate bad programming style. Object can be shared in one or another context.

However, this is not applicable to constants, and also for static immutable objects (which play the role of constants in cases where constant is not allowed by syntax, for example, if you want to have an immutable array of something, or any other object which is initialized only once and cannot be modified).

As constants cannot be modified, no one carries if they are accessible in all contexts. However, nothing needs to be "global". Everything should have its context. Say, top-level type with public access modifier can be accessed from any assembly in your process, and internal (default for top-level type) will make the top-level type accessible in just your assembly.

You did not specify the scope you want to work with, so let's assume this is the whole assembly.

Now, let's define constant integers. It actually depends on the purpose. This is the most compact way:
internal enum MyValue {
    Something = 75,
    SomethingElse =90,
    //...
}



在这种方法中,每个常量都有自己的名字。这比定义一个类更紧凑:


In this approach, each constant has its name. This is more compact then defining a class:

internal static class MyDefinitionSet {
    internal static const Something = 75;
    internal static const SomethingElse = 90;
    //...
}



除了清晰和简单之外,枚举还有一个重要的好处:你可以枚举它们,即使不是100%直接的方式。请参阅我的文章,我将从一些众所周知的文章中解释背景和技术数量:枚举类型不枚举!解决.NET和语言限制 [ ^ ]。



主要问题是:你没有真正解释你想用这些整数做什么。也许你不需要他们的名字,只需要一个清单。您可以只列出一个列表,或两者都有。你需要使列表只读,但最好只是数组(当然实现 System.Generic.Collections.List< int>


In addition to clarity and simplicity, enumerations have important benefit: you can enumerate them, even though in not 100% straightforward way. Please see my article on the topic where I explain the background and number of techniques, starting from some well-known ones: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

The major problem is: you did not really explain what you want to do with those integers. Maybe you don't need their names, need just a list. You can have just a list, or both. You need to make the list readonly, but is should better be just the array (which of course implements System.Generic.Collections.List<int>:

internal static class MyDefinitionSet {
    internal static readonly int[] MyIntegers = new int[] {75, 90, /* ... */ };
}



取决于你想要什么,这些方法的组合也很有意义:


Depending on what you want, the combination of the approaches also can make a lot of sense:

internal static class MyDefinitionSet {
    internal static readonly int[] MyIntegers = new int[] {
        MyValue.Something,
        MyValue.SomethingElse,
        //...
        };
}





-SA


这篇关于如何在vb中声明一个全局常量列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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