C# Setting Properties using Index

Posted by Guazz on Stack Overflow See other posts from Stack Overflow or by Guazz
Published on 2010-04-04T14:14:44Z Indexed on 2010/04/04 14:23 UTC
Read the original article Hit count: 534

Filed under:
|
|
|
|

I have a business class that contains many properties for various stock-exchange price types. This is a sample of the class:

public class Prices
{
    public decimal Today {get; set}
    public decimal OneDay {get; set}
    public decimal SixDay {get; set}
    public decimal TenDay {get; set}
    public decimal TwelveDay {get; set}
    public decimal OneDayAdjusted {get; set;}
    public decimal SixDayAdjusted {get; set;}
    public decimal TenDayAdjusted {get; set;}
    public decimal OneHundredDayAdjusted {get; set;}
}

I have a legacy system that supplies the prices using string ids to identify the price type.

E.g.

Today = "0D"  
OneDay = "1D"  
SixDay = "6D"  
//..., etc.   

Firstly, I load all the values to an IDictionary() collection so we have:

[KEY] VALUE
[0D] => 1.23456
[1D] => 1.23456
[6D] => 1.23456
...., etc.

Secondly, I set the properties of the Prices class using a method that takes the above collection as a parameter like so:

SetPricesValues(IDictionary<string, decimal> pricesDictionary)  
{  
    // TODAY'S PRICE  
    string TODAY = "D0";  
    if (true == pricesDictionary.ContainsKey(TODAY))  
    {  
        this.Today = pricesDictionary[TODAY];  
    }  
    // OneDay PRICE  
    string ONE_DAY = "D1";  
    if (true == pricesDictionary.ContainsKey(ONE_DAY))  
    {  
         this.OneDay = pricesDictionary[ONE_DAY];  
    }  
//..., ..., etc., for each other property   
}  

Is there a more elegant technique to set a large amount of properties? Thanks, j

© Stack Overflow or respective owner

Related posts about c#

Related posts about code