如何在C#中重用一个方法

时间:2017-08-09 21:42:22

标签: c#

所以,在下面的代码中,在最后一个方法中,我想重新使用第二个public static Valute GetValuteByDate(DateTime date, string valuteCharCode),但我真的不明白要给出的参数。如您所见,我成功地在第二种方法中重复使用了第一种方法。知道如何在第三种方法中重复使用第二种方法吗?或许你有一些有用的信息?

public static class Api
{
    public static ValCurs GetValCursByDate(DateTime date)
    {
        var client = new RestClient("http://bnm.md"); //request

        var request = new RestRequest("ro/official_exchange_rates/get_xml=1&date="+date.ToString(), Method.GET); //request

        var response = client.Execute<ValCurs>(request);//deserialization

        if (response.ErrorException != null) { return null; } //throw exception

        return response.Data;
    }

    public static Valute GetValuteByDate(DateTime date, string valuteCharCode)
    {
        var curs = GetValCursByDate(date);

        Valute valuteByDate = curs.FirstOrDefault(valute => valute.CharCode.Equals(valuteCharCode));

        return valuteByDate;
    }

    public static Valute GetMaxValuteByPeriod(DateTime startDate, DateTime endDate, string charCode)
    {
        var maxVal = GetValuteByDate(**?**);
    }
}

2 个答案:

答案 0 :(得分:2)

public static class Api
{
public static ValCurs GetValCursByDate(DateTime date)
{
    var client = new RestClient("http://bnm.md"); //request

    var request = new RestRequest("ro/official_exchange_rates/get_xml=1&date="+date.ToString(), Method.GET); //request

    var response = client.Execute<ValCurs>(request);//deserialization

    if (response.ErrorException != null) { return null; } //throw exception

    return response.Data;
}

public static Valute GetValuteByDate(DateTime date, string valuteCharCode)
{
    var curs = GetValCursByDate(date);

    Valute valuteByDate = curs.FirstOrDefault(valute => valute.CharCode.Equals(valuteCharCode));

    return valuteByDate;
}

public static Valute GetMaxValuteByPeriod(DateTime startDate, DateTime endDate, string charCode)
{
    var totalDays = (endDate-startDate).TotalDays;
    List<Valute> result = new List<Valute>(totalDays);
    for(int i = 0; i < totalDays; i++)
    {
         result.Add(GetValuteByDate(startDate.AddDays(i), charCode);
    }
    var maxVal = result.Max(p => p.<put here property>);

    return maxVal;
}
}

答案 1 :(得分:1)

第三个似乎是在使用范围,因此您需要在该范围内的每一天拨打第二个。

  for(var day = startDate; date<= endDate; day = day.AddDays(1))
    {
        Valute value = GetValuteByDate(date, valuteCharCode);
        //compare value to the max value and set if higher
    }

注意:我没有测试此代码,因此您可能不得不使用它