By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 1,527 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

How to know inside a constructor who called the constructor?

P: n/a
I have a Win Form app, calling the consctructor of a class. Inside
the class, I declare some stuff like new Label, etc. I want to add
this label inside the constructor in the windows form by
xxx.Control.Add(label). Sure I can pass this.Control in parameter in
my constructor from my windows form and it works, but I don't want to
do that. Is there any method or function that allow retreive who
call the constructor?

I also don't want to use User Control.

there is a sample of what I am trying to do

{
...
// MyApp Winform
MyCie.MyProd.MyGroupLabel glabel = new MyCie.MyProd.MyGroupLabel()
...
}

// Class MyGroupLabel
MyCie.MyProd.MyGroupLabel
{
class MyGroupLabel
{
public MyGroupLabel()
{
Label tomatoLabel = new Label();
Label potatoeLabel = new Label();

tomatoLabel.Text = "Tomatoe";
potatoeLabel.Text = "Potatoe";
tomatoLabel.SetBounds(.....);
potatoeLabel.SetBounds(.....);

// Get the object that call this constructor ... how to do that!?
// and add it in the windows form at set bounds
UnknownFunctionGetxxx().Controls.Add(tomatoLabel );
UnknownFunctionGetxxx().Controls.Add(potatoeLabel) ;

}
}

}

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Efkas <fl**@rfranco-dot-ca.no-spam.invalid> wrote:
I have a Win Form app, calling the consctructor of a class. Inside
the class, I declare some stuff like new Label, etc. I want to add
this label inside the constructor in the windows form by
xxx.Control.Add(label). Sure I can pass this.Control in parameter in
my constructor from my windows form and it works, but I don't want to
do that. Is there any method or function that allow retreive who
call the constructor?


No. You have to pass it in if you want to know it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
"Efkas" <fl**@rfranco-dot-ca.no-spam.invalid> wrote in message
news:41********@Usenet.com...
I have a Win Form app, calling the consctructor of a class. Inside
the class, I declare some stuff like new Label, etc. I want to add
this label inside the constructor in the windows form by
xxx.Control.Add(label). Sure I can pass this.Control in parameter in
my constructor from my windows form and it works, but I don't want to
do that.
Why not? It's simple and self-explanatory, and it works. Just pass "this".
Is there any method or function that allow retreive who
call the constructor?


I realize you're looking for some built-in thing like a hypothetical
FormManager.CurrentlyActiveForm but it just doesn't exist in the framework.
As far as I know the best you can do is walk the stack to figure it out, and
that would not be very performant -- and then you'd still have the problem
of converting the string representation of the fully qualified class name
into not just *an* instance of that class, but *the* instance you're looking
for.

No method (including a constructor) is clairvoyant -- you have to provide it
with the references and data it needs to do its work, either through
arguments or previously established class members or through (sometimes
undesireable) coupling to static members of other classes that act as
pseudo-globals.

The best I can do is suggest you have a singleton form manager class that
launches all your forms and keeps track of what form is currently active.
This could expose a collection of forms and a reference to the currently
active form as static members. But I can't see that it would be worth the
effort, at least not to solve this particular "problem". Indeed I'd think
you'd create more problems than you'd solve. Passing "this" to the
constructor is bullet-proof, simple, clean and self-documenting. Using some
composition of other mechanisms would be complex and relatively error-prone,
completely violating the KISS principle.

--Bob
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.