计算实例数 [英] count number of instances

查看:110
本文介绍了计算实例数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算在应用程序中创建的实例数?

例如:
有2种形式:form1,form2
在form1上有一个按钮.当我单击它时,将创建一个类型为form2的新表单.

我想在每次单击按钮时显示一个消息框,其中显示创建的form2实例的数量.

有帮助吗?

how do i count the number of instances created in an application?

eg:
there are 2 forms : form1, form2
There is a button on form1. When i click on it a new form of type form2 is created.

i want to display a message box displaying the number of form2 instances created, every time the button is clicked.

any help plz?

推荐答案

保留一个变量,该变量在每次创建新的form2实例时都会增加计数.
Keep a variable that increments the count everytime a new form2 instance is created.


要添加到上一个答案,您可以通过在窗体的构造函数中递增该变量来实现,对于创建的每个实例都会调用该变量.
To add to the previous answer, you would do that by incrementing that variable in the form''s constructor, as that is called for every instance created.


声明静态变量,并在每次单击按钮时对其进行递增,并在消息框中显示它:)
Declare static variable and increment it on every button click, and show it on message box :)


这篇关于计算实例数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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