How to know inside a constructor who called the constructor?

E

Efkas

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
*-----------------------*
 
J

Jon Skeet [C# MVP]

Efkas said:
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.
 
B

Bob Grommes

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
 

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

Top