Web Services 如何调用数组地另一个方法?

iqih9akk  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(111)

我正在尝试计算易货系统,所以首先我将项目添加到库存(json文件)另一种方法...然后尝试计算CalCost方法中的最小最大价格数组,并发送到howmuch方法,如下所示

[WebMethod]
    public double[] CalInstrumentCost(String instrumentName, double instrumentQuantity)
    {
        double[] priv = new double[2];

        try
        {

            string jsonString = File.ReadAllText(filePath);
            MusicInstrumentsList Mlist = JsonConvert.DeserializeObject<MusicInstrumentsList>(jsonString);
            var MInstrument = Mlist.MusicInstruments_List.Find(item => item.InstrumentsName == instrumentName);

            if (MInstrument != null)
            {

                double miprice = MInstrument.MinInstrumentPrice * instrumentQuantity;
                double maPrice = MInstrument.MaxInstrumentPrice * instrumentQuantity;

                priv[0] = miprice; // min Price
                priv[1] = maPrice; // max Price

                return priv;  
            }
            else
            {
                throw new InvalidOperationException("Invalid Operation");
            }
        }
        catch
        {
            throw new InvalidOperationException("Invalid Operation");
        }
    }

但priv不存在,所以我尝试全局定义,但计算结果是INF(1/0 = ⇒ nf),我该怎么办?

[WebMethod]
    public double HowMuchInstrument(String instrumentName, double totalBudget)
    {
        double result = 0;


        try
        {
            string jsonString = File.ReadAllText(filePath);
            MusicInstrumentsList Mlist = JsonConvert.DeserializeObject<MusicInstrumentsList>(jsonString);
            var MInstrument = Mlist.MusicInstruments_List.Find(item => item.InstrumentsName == instrumentName);

            if (MInstrument != null)
            {
                result = Math.Floor(totalBudget / priv[0]);
            }
            else
            {
                return result;
            }
        }
        catch
        {
            return result;
        }
        return result;
    }
3ks5zfa0

3ks5zfa01#

您在CalInstrumentCost()内部创建priv,以便在外部不可见。
您可以向HowMuchInstrument()添加其他参数并在内部调用CalInstrumentCost()(如果此方法在同一个类中):

// instrumentQuantity parameter added (do you really need double and not int ?)
[WebMethod]
public double HowMuchInstrument(String instrumentName, double instrumentQuantity, double totalBudget)
{
    double result = 0;

    try
    {
        string jsonString = File.ReadAllText(filePath);
        MusicInstrumentsList Mlist = JsonConvert.DeserializeObject<MusicInstrumentsList>(jsonString);
        var MInstrument = Mlist.MusicInstruments_List.Find(item => item.InstrumentsName == instrumentName);
         
        // here you call method to make needed calculations and store result in local variable
        var priv = CalInstrumentCost(instrumentName, instrumentQuantity);

        if (MInstrument != null)
        {
            result = Math.Floor(totalBudget / priv[0]); // no error here anymore
        }
        else
        {
            return result;
        }
    }
    catch
    {
        return result;
    }
    return result;
}

备注:

  1. instrumentQuantity的类型是double-不能是int
    1.在CalInstrumentCost()中使用数组来存储最小值和最大值-您只需创建具有2个属性单独类,而无需依赖数组中的位置
    1.如果需要更高的精度,可以使用decimal代替double

相关问题