简体   繁体   中英

C# What is the best way to compare a double value to a range of values and return two text values

I have a project that is in C# but I don't know the best way to use a value (Which can range from 1500 to -1500) to compare with a range of 9 values and return two string values associated with one of the 9 possible range values.

So far, I have done some research and I believe that a dictionary would be fastest to do this, so I created one as an example to show you the data:

class MyTICKObject
{
public double MyDouble  { get; set; }
public string MyString1 { get; set; }
public string MyString2 { get; set; }
}

private Dictionary<double, MyTICKObject>    dictTICK = new  Dictionary<double, MyTICKObject>();

int i = dictTICK.Keys.Count();      
    if( i == 0)
    {
        dictTICK.Add(1000,  new MyTICKObject { MyDouble=1000,  MyString1="Extreme Bullish", MyString2="DarkSeaGreen"});
        dictTICK.Add(600,   new MyTICKObject { MyDouble=600,   MyString1="Strong Bullish",  MyString2="Lime"});
        dictTICK.Add(400,   new MyTICKObject { MyDouble=400,   MyString1="Bullish",         MyString2="Green"});
        dictTICK.Add(100,   new MyTICKObject { MyDouble=100,   MyString1="Positive",        MyString2="DarkGreen"});
        dictTICK.Add(0,     new MyTICKObject { MyDouble=0,     MyString1="Neutral",         MyString2="DarkGray"});
        dictTICK.Add(-100,  new MyTICKObject { MyDouble=-100,  MyString1="Negative",        MyString2="DarkRed"});
        dictTICK.Add(-400,  new MyTICKObject { MyDouble=-400,  MyString1="Extreme Bearish", MyString2="FireBrick"});
        dictTICK.Add(-600,  new MyTICKObject { MyDouble=-600,  MyString1="Strong Bearish",  MyString2="Red"});
        dictTICK.Add(-1000, new MyTICKObject { MyDouble=-1000, MyString1="Extreme Bearish", MyString2="IndianRed"});
    }

What I want to do is compare the incoming TICK values (201,-12,456, etc.) with MyDouble, find it's position between two MyDouble values then return MyString2 to a variable called strStmnt, and return MyString2 to a variable called strColor.

Thank you for your help.

Instead of using a dictionary, just make a list (or array if you're happy to write it all at once) since you already ordered it and the MyTICK object contains the stop. Then you can just iterate through the list from top to bottom with a while loop until you reach the right range stop values. Also be aware that you've defined a set of stops and assigned names to each stop but really you want one less name than stops to be able to assign the in-between values of the input double.

I've given an example below which wouldn't work very well because of the stop-values == names problem I mentioned but it should give you the right idea.

private MyTICKObject[] list = new MyTICKObject[]
            {
                new MyTICKObject { MyDouble = 1000, MyString1 = "Extreme Bullish", MyString2 = "DarkSeaGreen" },
                new MyTICKObject { MyDouble = 600, MyString1 = "Strong Bullish", MyString2 = "Lime" },
                new MyTICKObject { MyDouble = 400, MyString1 = "Bullish", MyString2 = "Green" },
                new MyTICKObject { MyDouble = 100, MyString1 = "Positive", MyString2 = "DarkGreen" },
                new MyTICKObject { MyDouble = 0, MyString1 = "Neutral", MyString2 = "DarkGray" },
                new MyTICKObject { MyDouble = -100, MyString1 = "Negative", MyString2 = "DarkRed" },
                new MyTICKObject { MyDouble = -400, MyString1 = "Extreme Bearish", MyString2 = "FireBrick" },
                new MyTICKObject { MyDouble = -600, MyString1 = "Strong Bearish", MyString2 = "Red" },
                new MyTICKObject { MyDouble = -1000, MyString1 = "Extreme Bearish", MyString2 = "IndianRed" },
            };

public MyTICKObject findAt(double value)
    {
        foreach (MyTICKObject tick in list)
        {
            if (value < tick.MyDouble)
            {
                return tick;
            }
        }
    throw new ArgumentException("Value out of range");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM