以窗口形式调用方法

时间:2013-10-05 15:35:57

标签: c#

通过运行我的代码,我收到此错误 方法必须有一个返回类型
它在我的公共撤销方法底部。我可能没有正确地理解这一点,但我认为我的回归是可以的,因为我的访问器和mutator方法正在这样做。另外,我觉得自从我使用aMtBox.Text = a.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;
    }
}

2 个答案:

答案 0 :(得分: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
        }

答案 1 :(得分:2)

您收到此错误方法必须具有返回类型,因为您没有返回类型

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();

<小时/> @As在评论中提到我正在编写此代码,它不是原始答案的一部分

 /// <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");
            }
        }