471,350 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Static Method Question

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
Jan 19 '06 #1
3 1255
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Doug Handler" <dk*******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Jan 19 '06 #2
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.
"Doug Handler" <dk*******@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Jan 19 '06 #3
Hello Doug,

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

så panel need to be static as well.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
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

Jan 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Murat Tasan | last post: by
1 post views Thread by baylor | last post: by
10 posts views Thread by Marek | last post: by
12 posts views Thread by chandu | last post: by
37 posts views Thread by minkoo.seo | last post: by
reply views Thread by Joe Strout | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.