Static Method Question

  • Thread starter Thread starter Doug Handler
  • Start date Start date
D

Doug Handler

I have a static method that needs to assign a type to an instance of an
object. What's the best way to address this?

e.g.

private Panel pnlMain = new Panel();

.....

public static void SetControl(Button btnInfo)
{
// pseudo-code - I need this to happen here - how can I do this. The
method needs to be a static one.
this.pnlMain.Controls.Add(btnInfo);

}

Doug
 
Doug,

You can't do it in a static method, you would have to pass pnlMain to
the method, like so:

public static void SetControl(Control control, Button btnInfo)
{
control.Controls.Add(btnInfo);
}

Honestly, this doesn't warrant a static method. I would just do this in
an instance method on your control.

Hope this helps.
 
You can either pass in an instance of the parent control that you can work
against, or have some other class maintain a reference to the parent class
that you can then ask for. Probably passing the parent into the method is
the simplest solution.
 
Hello Doug,

you can't. Static methods can only access static variables.

så panel need to be static as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top