You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForms中Money类金额操作异常:赋值而非增减问题排查

问题排查:C# WinForms存款操作未正确累加/递减余额

问题场景

在Visual Studio开发带余额分析功能的C# WinForms校园项目,期望实现存款操作:从CryptoMoney扣除指定金额并添加至WalletMoney,但实际执行时仅为赋值而非累加/递减。

相关代码

点击事件代码

private void DepositBtn_Click_1(object sender, EventArgs e)
{
    int num = Int32.Parse(textBoxDeposit.Text); // 用户输入金额的文本框

    if (obj.Cryptomoney >= num)
    {
        obj.SubtractCryptoMoney(num);
        MessageBox.Show(obj.Cryptomoney.ToString());
        obj.AddWalletMoney(num);
        MessageBox.Show(obj.Walletmoney.ToString());

    }
    else
    {
        MessageBox.Show("error");
    }
}

Money类代码

internal class Money
{
    // 管理员在登录界面设置的金额,用于视频相关功能
    int CryptoMoney;    // 加密钱包金额
    int WalletMoney;   // 软件钱包金额
    int EarnedMoney;  // 近30天赚取的金额
   
    public Money(int cryptoMoney, int walletMoney, int earnedMoney)
    {
        CryptoMoney = cryptoMoney;
        WalletMoney = walletMoney;
        Earnedmoney = earnedMoney;
    }
    public int Earnedmoney
    {
        get { return EarnedMoney; }
        set { EarnedMoney = value; }
    }
    public int Cryptomoney
    {
        get { return EarnedMoney; }
        set { EarnedMoney = value; }
    }
    public int Walletmoney
    {
        get { return EarnedMoney; }
        set { EarnedMoney = value; }
    }
}

金额操作方法

// 点击存款按钮时调用的方法
public int SubtractCryptoMoney(int num) { Cryptomoney -= num; return Cryptomoney; }
public int SubtractWalletMoney(int num) { Walletmoney -= num; return Walletmoney; }
public int AddCryptoMoney(int num) { Cryptomoney += num; return Cryptomoney; }
public int AddWalletMoney(int num) { Walletmoney += num; return Walletmoney; }

问题原因

核心错误出在Money类的属性映射错误:所有公开属性(Cryptomoney、Walletmoney、Earnedmoney)的读写逻辑都指向了同一个私有字段EarnedMoney,而非各自对应的私有字段:

  • Cryptomoney的get/set未关联类中定义的CryptoMoney字段,而是错误指向EarnedMoney
  • Walletmoney的get/set同样错误指向EarnedMoney,而非WalletMoney字段

这导致所有余额操作实际都在修改EarnedMoney的值,完全没有操作到CryptoMoney和WalletMoney,自然无法实现预期的转账逻辑。

修复方案

修正Money类的属性映射,让每个公开属性对应正确的私有字段:

internal class Money
{
    // 管理员在登录界面设置的金额,用于视频相关功能
    int CryptoMoney;    // 加密钱包金额
    int WalletMoney;   // 软件钱包金额
    int EarnedMoney;  // 近30天赚取的金额
   
    public Money(int cryptoMoney, int walletMoney, int earnedMoney)
    {
        CryptoMoney = cryptoMoney;
        WalletMoney = walletMoney;
        Earnedmoney = earnedMoney;
    }
    public int Earnedmoney
    {
        get { return EarnedMoney; }
        set { EarnedMoney = value; }
    }
    // 修正:关联到正确的私有字段CryptoMoney
    public int Cryptomoney
    {
        get { return CryptoMoney; }
        set { CryptoMoney = value; }
    }
    // 修正:关联到正确的私有字段WalletMoney
    public int Walletmoney
    {
        get { return WalletMoney; }
        set { WalletMoney = value; }
    }

    // 点击存款按钮时调用的方法
    public int SubtractCryptoMoney(int num) { Cryptomoney -= num; return Cryptomoney; }
    public int SubtractWalletMoney(int num) { Walletmoney -= num; return Walletmoney; }
    public int AddCryptoMoney(int num) { Cryptomoney += num; return Cryptomoney; }
    public int AddWalletMoney(int num) { Walletmoney += num; return Walletmoney; }
}

修复后,调用SubtractCryptoMoney会正确扣除CryptoMoney的值,AddWalletMoney会正确累加WalletMoney的值,实现预期的转账逻辑。

额外提示:建议对输入金额做合法性校验,比如用int.TryParse替代Int32.Parse,避免空输入或非数字输入抛出异常。

内容的提问来源于stack exchange,提问作者EmilHaKarish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 06:32:36