对非共享成员的引用需要一个对象引用 - 在我的winform上获取错误 [英] Reference to a non-shared member requires an object reference - Getting error on my winform

查看:120
本文介绍了对非共享成员的引用需要一个对象引用 - 在我的winform上获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:



我的form1是我的主要形式,form2是一个在form1上弹出的子表单。 Form2有一个用于关闭form2和form1的按钮。我可以关闭form2但是当我写的时候



Hello:

I have form1 that is my main form and form2 as a sub form that pops up on on form1. Form2 has a button that is used to close form2 and form1. I am able to close form2 but when I write

form2.Close() 





我收到上面的错误。



我之前从未见过这个错误,但我从来没有关闭过以前从另一种形式的形式。我已经研究过这个问题,但是我无法做出正面或反面。



I get the error above.

I have never seen this error before, but I have never had to close a form from another form before. I have researched this issue, but I have not been able to make heads or tails of it.

推荐答案

form2 是一个类,而不是一个引用类实例的变量,所以你不能调用它的Close方法,除非它被声明为共享 - 它没有' t,因为你想要关闭 form2 的特定实例,而不是一般的类。



如果你试图从类中关闭 form2 的当前实例,然后直接使用Close方法:

form2 is a class, not a variable referring to an instance of a class, so you can't call it's Close method unless it has been declared as Shared - which it hasn't, because you want to close a specific instance of form2, not the class in general.

If you are trying to close the current instance of form2 from within the class, then use use the Close method, either directly:
Close()

或通过当前实例说明符

Or via the current instance specifier Me:

Me.Close()


这篇关于对非共享成员的引用需要一个对象引用 - 在我的winform上获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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