如何在静态函数中访问控件属性 [英] How do I Access to control property in static function

查看:109
本文介绍了如何在静态函数中访问控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我想在静态函数中访问默认控件属性(例如:panel),



怎么做?

解决方案

这个方法毫无意义,因为它是静态的。它原则上不能做任何事情,因为除了它创建的对象之外它不能访问任何对象,并且不返回任何东西。重要的是,如果 pnlShowMessage 是某个实例成员(非静态),它甚至不会编译。如果由于任何原因,你使 pnlShowMessage 静态,这将更糟糕,一种典型的滥用。



请注意,您在此方法中创建的所有对象最终都将被销毁并进行垃圾回收,这些对象总是对无法访问的托管对象执行。



您需要了解什么是静态方法。这是一种无法访问其声明类型实例的方法。非静态方法也称为实例方法。它们是不同的:它们通过对象这个进行访问,它始终是对方法调用中使用的类型实例的引用(这种方法不能被调用)没有实例)。引用作为隐式第一个参数传递给方法,与所有其他元素的方式相同。如果你的 pnlShowMessage 被(希望)和实例成员,在一个实例方法它可以被作为 this.pnlShowMessage 访问,和这个可以在不产生歧义的情况下省略。这就是静态方法毫无意义的原因。


不要误解我的意思:静态函数应该被使用并且非常重要。当它使用的所有对象作为方法参数显式传递时,应该使用它们,并且可选地,使用 ref 参数, out 参数或返回值。此外,这样的功能可以使用一些静态成员,最好避免使用。



现在,我建议你从一开始就审查你对编程的理解,从类型开始和实例,变量,方法和方法调用,等等。您在最基本的概念中迷失了方向,因此您需要先解决这个问题,然后才能进一步移动。我建议停止UI开发,直到你对写一些仅限控制台的样本有足够的信心。



-SA

hi all

I wanna Access to default control property (eg:panel) as following in static function ,

How do it?

解决方案

This method make no sense at all, because it is static. It cannot do anything in principle, because it has no access to any objects except the objects it creates, and does not return anything. Importantly, it won't even compile if pnlShowMessage is some instance member (non-static). And if, by any reason, you made pnlShowMessage static, this would be even worse, a typical abuse.

Note that all objects you created in this method will be eventually destroyed and garbage-collected, which is always done to managed objects which become unreachable.

You need to understand what is a static method. This is a method which has no access to the instance of the type it is declared in. Non-static methods are also called "instance methods". They are different: they have such access through the object this, which is always a reference to the instance of the type used in the call to the method (such method cannot be called without an instance). The reference this is passed to the method as implicit first argument, the same way as all other elements. If your pnlShowMessage is (hopefully) and instance member, in an instance method it could be accessed as this.pnlShowMessage, and this could be omitted in cases where it does not create ambiguity. That's why a static method makes no sense.

Don't get me wrong: static functions should really be used and are very important. They should be used when all the objects it uses are passed explicitly as method arguments and, optionally, returned from the method using ref arguments, out arguments, or return value. Additionally, such function can use some static members, which is best avoided.

Now, I recommend you to review your understanding of programming from the very beginning, starting with types and instances, variables, methods and method calls, an so on. You are getting lost in the very basic notions, so you need to fix that before you can move any further. I would recommend to stop UI development until you gain enough confidence on writing just some console-only samples.

—SA


这篇关于如何在静态函数中访问控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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