Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
CHill60 5-Feb-14 14:31pm    
This sounds like homework. And the title of the question really isn't what you are asking. Have a go at it yourself - if you get stuck post your code and we'll try to help

1 solution

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:
C#
internal enum MyValue {
    Something = 75,
    SomethingElse =90,
    //...
}

In this approach, each constant has its name. This is more compact then defining a class:
C#
internal static class MyDefinitionSet {
    internal static const Something = 75;
    internal static const SomethingElse = 90;
    //...
}

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>:
C#
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:
C#
internal static class MyDefinitionSet {
    internal static readonly int[] MyIntegers = new int[] {
        MyValue.Something,
        MyValue.SomethingElse,
        //...
        };
}


—SA
 
Share this answer
 
v2
Comments
Maciej Los 5-Feb-14 15:11pm    
+5!
Sergey Alexandrovich Kryukov 5-Feb-14 15:14pm    
Thank you, Maciej.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900