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
Updated 15-Apr-15 13:19pm
v7