c# - 如何正确返回值?

标签 c# function

我正在开发一个应用程序,允许用户跟踪省钱目标。

public static int CalcProg(int userGoal, int userBalance, int userProg)
{
    userProg = userBalance / userGoal;
    userProg = userProg * 100
    return userProg;
}

private void Form1_Load(object sender, EventArgs e)
{
    //Calls the FileVerification Method
    FileVerification();

    //Sets the label1 transparency to true
    label1.Parent = pictureBox1;
    label1.BackColor = Color.Transparent;

    LoadData();
    CalcProg(userGoal, userBalance, userProg);

    progressBar1.Value = userProg;
    progLabel = Convert.ToString(userProg);
    label3.Text = progLabel;
}

这只是代码的一小部分,但这是我遇到问题的地方。

我使用方法来读取和写入变量 userBalance 和 userGoal 中使用的数据的文件。这一切都工作正常,因为当我在下面的转换函数中使用这些变量之一而不是“userProg”时,它的显示就像在文本文件中一样。

当我尝试进行转换时,我的问题出现了。我的公式在 CalcProg 中。当我实际启动程序时,在变量 userProg 上设置其值的两个元素(进度条和标签)仅显示零,无论在文本文件中输入什么值。

我尝试对 CalcProg 方法使用 double 型并将 userProg 设置为 double 型,但这不起作用。我有点卡住了,如果有人可以帮助我,我将不胜感激。

最佳答案

实际的问题是,您从方法中返回一个 int,但您从未使用它。 将代码更改为:

public static int CalcProg(int userGoal, int userBalance, int userProg)
{
    userProg = userBalance / userGoal;
    userProg = userProg * 100
    return userProg;

}

private void Form1_Load(object sender, EventArgs e)
{
    //Calls the FileVerification Method
    FileVerification();
    //Sets the label1 transparency to true
    label1.Parent = pictureBox1;
    label1.BackColor = Color.Transparent;
    LoadData();

    progressBar1.Value = CalcProg(userGoal, userBalance, userProg);
    progLabel = Convert.ToString(userProg);
    label3.Text = progLabel;


}

关于c# - 如何正确返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18628022/

相关文章:

Python:字面意思是 "printing"一个函数

function - Autohotkey 如何使用函数参数进行多个图像搜索

PHP:收集传递给函数的所有变量作为数组?

c# - 如何减少页面加载时间(结合javascript)?

c# - 如何用简单的英语解释这个 lambda 表达式?

c# - 属性私有(private)集崩溃统一

javascript - jQuery:使用普通函数值作为 jquery 值

python - 通过另一个函数访问一个函数的返回值

c# - 我可以反序列化为 protobuf-net 中接口(interface)的只读属性吗?

c# - 格式化 TextBlock 中的文本