在窗口窗体中调用方法

本文关键字:调用 方法 窗体 窗口 | 更新日期: 2023-09-27 18:34:06

通过运行我的代码,我收到此错误方法必须具有返回类型
它在底部的公开提款方法中。我可能没有正确理解这一点,但我认为我的返回是可以的,因为我的访问器和突变器方法正在这样做。另外,我觉得因为我使用 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);
        }
    }
}


我的银行账户代码如下

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;
    }
}

在窗口窗体中调用方法

此方法必须具有返回类型

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
        }

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

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