简体   繁体   中英

How to re-use a method in C#

So, in the code below, In the last method, I want to re use the second one public static Valute GetValuteByDate(DateTime date, string valuteCharCode) , but I really don't understand what parameters to give. As you can see, I successfully re-used first method in the second method. Any idea what I can do to re-use the second method in the third one? Or maybe you have some useful information?

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(**?**);
    }
}
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;
}
}

The third one seems to be using a range, so you'd need to call the second one for each day in the range.

  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
    }

Note: I didn't test this code so you might have to fiddle with it

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