【问题标题】:Call the method in the constructor after the initialization初始化后调用构造函数中的方法
【发布时间】:2021-01-21 14:13:48
【问题描述】:

应该在 Trade 对象初始化之后立即调用 CalcTradePrice 方法。现在,它在此之前被调用,这导致了一个异常:

System.DivideByZeroException: '试图除以零。'

var trade = new Trade
{
    Pair = pair,
    OpenRate = buyCandle.Close,
    OpenDate = buyCandle.OpenTime,
    Amount = 12345,
    OpenFee = _backtestOptions.OpenFee,
    CloseFee = _backtestOptions.CloseFee,
    IsOpen = true
};

我知道我可以在初始化之后手动调用该方法,就像这样:

var trade = new Trade
{
    Pair = pair
};
CalcTradePrice();

或创建一个执行它的派生类,但我不想这样做。有什么建议吗?

贸易类:

public class Trade
{
    public string Pair { get; set; }
    public decimal OpenRate { get; set; }
    public decimal CloseRate { get; set; }
    public DateTime OpenDate { get; set; }
    public DateTime CloseDate { get; set; }
    public decimal OpenTradePrice { get; set; }
    public decimal Amount { get; set; }
    public decimal OpenFee { get; set; }
    public decimal CloseFee { get; set; }
    public SellType SellType { get; set; }
    
    public Trade()
    {
        CalcTradePrice();
    }
    
    public void CalcTradePrice()
    {
        this.OpenTradePrice = ... // DivideByZeroException because all elements are null at that moment
    }
}

【问题讨论】:

  • 这能回答你的问题吗? Try catch in c# for divide by zero error
  • Amount = currentBalance / buyCandle.Close, 使Amount 成为一个函数,而不是一个属性。将 CurrentBalance 存储为属性。
  • @SergeyBerezovskiy,忽略 Amount 属性,事实并非如此,因为我试图将其设置为硬编码值。问题是构造函数在new Trade { Pair = pair, .... }之前被调用。

标签: c# oop design-patterns


【解决方案1】:

您可以创建参数化构造函数。通过传递给构造函数的参数分配所有属性。使用这些属性在CalcTradePrice()函数中计算交易价格。

public class Trade
{
    ...
    
    //I just passed two properties as an example, you can pass all required properties.
    public Trade(int currentBalance, int candleCloseValue)
    {
        this.CurrentBalance = currentBalance;
        this.CandleCloseValue= candleCloseValue;
        CalcTradePrice();
    }
    
    public void CalcTradePrice()
    {
        this.OpenTradePrice = this.CurrentBalance/this.CandleCloseValue; 
    }
}

在创建 Trade 类的对象时,将值作为参数传递,例如,

var currentBalance = 100;
var candleCloseValue = 20;  
Trade trade = new Trade(currentBalance , candleCloseValue);

注意:我使用以上两个属性只是为了说明,在调用CalcTradePrice() 函数之前如何分配属性值。

【讨论】:

  • 谢谢!这是最好的答案
  • 很高兴,我的回答对你有帮助;)
【解决方案2】:

这个呢:

public class Trade
{
    public string Pair { get; set; }
    public decimal OpenRate { get; set; }
    public decimal CloseRate { get; set; }
    public DateTime OpenDate { get; set; }
    public DateTime CloseDate { get; set; }
    public decimal OpenTradePrice { get; set; }
    public decimal Amount { get; set; }
    public decimal OpenFee { get; set; }
    public decimal CloseFee { get; set; }
    public SellType SellType { get; set; }
    
    public Trade(buyCandle,_backtestOptions, ..and so on )
    {

    Pair = pair,
    OpenRate = buyCandle.Close;
    OpenDate = buyCandle.OpenTime;
    Amount = currentBalance / buyCandle.Close;
    OpenFee = _backtestOptions.OpenFee;
    CloseFee = _backtestOptions.CloseFee;
    IsOpen = true;

     CalcTradePrice();

     }
};

【讨论】:

    【解决方案3】:

    你的例外System.DivideByZeroException: 'Attempted to divide by zero.' 可能由

    触发

    Amount = currentBalance / buyCandle.Close

    而不是你的构造函数调用。

    【讨论】:

    • 不,不是。我试图输入一个硬编码的值。
    猜你喜欢
    • 2013-04-01
    • 2012-04-09
    • 2018-06-23
    • 2014-06-08
    • 2018-08-02
    • 2018-07-27
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多