0

通过运行我的代码,我收到此错误 Method must have a return type
它在底部的我的公共撤回方法中。我可能没有正确理解这一点,但我认为我的回报是可以的,因为我的访问器和突变器方法正在这样做。另外,我觉得因为我使用了 aMtBox.Text = a.withdraw(withdraw); 它不会正确地投射它(或者需要被投射?)。我还没有关于它的错误,但我猜是因为我还没有解决退货问题。

因此,以一种更简单的方式,我如何调用我的withdraw 方法以在我的withdraw_Click 中正确使用。

我的主表单代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        BankAccount a = new BankAccount();

        public void withdrawl_Click(object sender, EventArgs e)
        {
            aMtBox.Text = a.withdraw(withdraw);
        }
    }
}


我的 BankAccount 代码如下

public class BankAccount
{
    decimal num1;
    decimal iBa;
    decimal total;

    public decimal IBa
    {
        set { iBa = value; }
        get { return iBa; }
    }
    public decimal Num1
    {
        set { num1 = value; }
        get { return num1; }
    }
    public decimal Total
    {
        set { total = value; }
        get { return total; }
    }
    public decimal withdraw(decimal withdraw)
    {
        num1 = 0.00m;
        iBa = 300.00m;
        total = iBa - num1;
        return total;
    }
}
4

2 回答 2

2

此方法必须具有返回类型

public withdraw(decimal withdraw)
        {
            num1 = 0.00m;
            iBa = 300.00m;
            total = iBa - num1;
        }

喜欢

public decimal withdraw(decimal withdraw)
        {
            num1 = 0.00m;
            iBa = 300.00m;
            total = iBa - num1;
            return total;//or whatever
        }
于 2013-10-05T15:41:39.210 回答
2

您收到此错误Method must have a return type因为您没有返回类型

public withdraw(decimal withdraw)
    {
        num1 = 0.00m;
        iBa = 300.00m;
        total = iBa - num1;
    }

现在,如您所知,您可以通过为其提供返回类型来解决问题,
现在的问题是您将提供哪种返回类型stringdecimal
我的建议是使用十进制,然后尝试将其转换为字符串,同时将其分配给任何字符串属性,就像这里一样aMtBox.Text = a.withdraw(withdraw);


你的代码看起来像

    public decimal withdraw(decimal withdraw)
    {
        num1 = 0.00m;
        iBa = 300.00m;
        total = iBa - num1;
        return total;
    }


当你得到它时使用aMtBox.Text = a.withdraw(withdraw).ToString();
编辑
不要忘记在你的代码中声明变量撤回
你的代码看起来像

Decimal withdraw = 100; 
aMtBox.Text = a.withdraw(withdraw).ToString();


@正如评论中所问的,我正在编写此代码,它不是原始答案的一部分

 /// <summary>
 /// Called when user change textbox value
 /// </summary>
 /// <param name="sender">Represent object of sender (i.e text box itself)</param>
 /// <param name="e">Reesent eventArgs</param>
 private void withBox_TextChanged(object sender, EventArgs e)
        {
            TextBox tmpTextBox = (TextBox)sender; // Doing type casting sender to textbox so that we can access property of it.
            decimal withdraw;
            bool bIsParsed = Decimal.TryParse(tmpTextBox.Text, out withdraw); // TryParse method return false if there is any problem in process of doing parsing of string to decimal
            if (!bIsParsed)
            {
                MessageBox.Show("Please enter valid number");
            }
        }
于 2013-10-05T15:59:52.523 回答