功能不响应 [英] Function Does Not Respond

查看:91
本文介绍了功能不响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#编程的新手。我不知道我的代码有什么问题。这是名为'main'的类

I am new to C# programming. I dont know what is wrong with my code. This is class named 'main'

namespace ParkingLot
{


    public partial class main : Form
    {
        //global private integer
        public int number;

        public main()
        {
            InitializeComponent();
            number = 165;
            numberlabel.Text = number.ToString();


        }
        public void decrement()
        {
            //decrement on click
            number--;
            //update label
            numberlabel.Text = number.ToString();

            if (number <= 0)
            {
                MessageBox.Show("Parking Lot is full");
            }

        }





我在另一个班级调用了函数减量



And I called the function decrement in another class

private void button1_Click(object sender, EventArgs e)
       {
          //new object for decrement
            main objdec = new main();
            objdec.decrement();
        }



问题是函数'减量'没有响应。当我删除其他所有内容并只是调用函数然后它响应但是当我做像上面我没有得到任何回应。可能是什么问题?


The problem is that the function 'decrement' is not responding.When I remove everything else and simply call the function then it responds but when I do like above I don't get any respond. What may be the problem?

推荐答案

很明显,如果你考虑一下:单词 new 是一个非常大的线索!



执行该行时:

It's pretty obvious, if you think about it: the word new is a pretty big clue!

When you execute the line:
main objdec = new main();

它会为您的表单创建一个全新的,随时可用的实例,并设置一个新值number到$ 165.

然后用它来减少:

It creates a brand new, ready to use instance of your main form, with a new value of "number" which is set to 165.
You then use this to decrement:

objdec.decrement();

因此它的价值降至164,其数字标签显示该价值。然后你的方法结束,所以它超出了范围并将被销毁。在任何时候它都没有显示,或者其他任何事情都没有显示,所以你永远不会看到结果,也永远不会得到MessageBox。



可能,你想要处理完全创建新main的行,而不是使用 objdec 您想要使用您正在显示的表单的实际实例!



这有点像汽车:如果你把手机放在汽车的手套箱里,然后买一辆新车,你会不会想要将手机放在新车的手套箱里? br />


缺少< pre>标签,拼写错误[/ edit]

So it's value drops to 164, and its numberlabel shows that value. Your method then ends, so it goes out of scope and will be destroyed. At no time it is displayed, or anything else done with it, so you never see the result, and never, ever get the MessageBox.

Probably, you want to dispose of the line creating a new main entirely, and instead of using objdec you want to use the actual instance of the form that you are displaying!

It's a bit like cars: if you put your mobile phone in the glove box of your car, then buy a new car, would you expect the phone to be in the new cars glove box?

[edit]Missing <pre> tag, spelling error[/edit]


这篇关于功能不响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆