Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi all

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

How do it?
Posted
Updated 16-Apr-15 2:46am
v3
Comments
Sascha Lefèvre 15-Apr-15 17:37pm    
Please tell why this other function(dialogresult) needs to be static..?

1 solution

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
 
Share this answer
 
v7
Comments
Sascha Lefèvre 15-Apr-15 18:53pm    
My 5!
I believe there's the word "static" missing in the last sentence of the first paragraph.
/Sascha
Sergey Alexandrovich Kryukov 15-Apr-15 19:14pm    
This is exactly what I meant to write. Fixed, as well as few other places.
Thank you very much, Sascha.
—SA
bernova 16-Apr-15 1:42am    
this function run correctly without static modifier , but in this function i define pnlshow control that shows button

my exactly question :
how do i define the control inform static modifier?
Sergey Alexandrovich Kryukov 16-Apr-15 1:46am    
I even explained why. I already answered your question. Can you see it.
I don't understand what is "control inform static modifier".
I clearly explained why you cannot reasonably use static method. Forget it. I mean, learn programming and the role of static, but forget you idea, it's wrong.
—SA
bernova 16-Apr-15 1:50am    
-SA thanks for your response.
I write customize message box in C# and run correctly and use it in the N-layer project (team work) for pretty UI. all of thing is ok.

for access to static modifier :
if that is variable we could use auto-Implementation ( that i use this and run correctly) .
or if it be function we could use namespace.classname.static-function that it run correctly but for default control(eg:panel) don't be.

thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900