473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to know inside a constructor who called the constructor?

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
2 1556
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
1
by: Silver | last post by:
Hi everyone, I'm writing a program that has to do them following main() : ------- 3 objects are declared (two of type A and one of type B) The function frd_fun is called for two objects (B...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
8
by: arun | last post by:
Hello Group, I have a class class Simple2 { publica: Simple2(); list<int> *l1; list<int> l2; };
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
1
by: BrianMH | last post by:
I'm trying to access some of the global's inside my class LiveSearch and I have no idea how to go about it. Here is what I have so far: <script type="text/javascript" src="query.js"></script>...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.