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

Want to take the path of specific overloaded constructors from derived class up to just one below the base class.

P: n/a

The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain.
I am calling the class at the bottom, "public class D" from a client app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------


I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hopefully explaining what this() and base() mean will clarify what's happening. When you have 2 constructors like the following:

public B() { ... }
public B(Token tkn) : this() { ... }

what that means is the second constructor will first invoke the zero-argument constructor for the class and then execute it's own code. Thus, if they were

public B() { this.a = 1; }
public B(Token tkn) : this() { this.b = 2; }

the second constructor is equivalent to:

public B(Token tkn) { this.a = 1; this.b = 2; }

Putting the repeated code in the first contructor promotes code reuse.

Now, whenever you construct a class that's a derived class, a constructor on the base class MUST be invoked. By default, it invokes the zero-argument contructor, assuming it's defined and not private. However, your derived class can cause a different constructor on the base class to be called by using the base() syntax. For example, in the class C, you have

public C(Token tkn) : base(tkn)

That tells the runtime to invoke the base class contructor that takes a Token argument (i.e. B(Token tkn)). Also, keep in mind that the base class constructor is always called BEFORE the derived class's constructor.

So, the full path of your constructor when you invoke "new D(tkn)" is:

A();
B();
C();
D(Token);

However, if you change your definition of the second constructor for D to be:

public D(Token tkn) : base(tkn)

then your constructor sequence would be:

A();
B();
B(Token);
C(Token);
D(Token);

which is probably more what you're looking for.

Ken
"hazz" <ha**@sonic.net> wrote in message news:uu**************@TK2MSFTNGP10.phx.gbl...

The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain.
I am calling the class at the bottom, "public class D" from a client app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------
I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #2

P: n/a
Super explanation Ken. Thank you ! I get that. After walking through your very clear explanation, I can't wait to swing by work this weekend and try that out. I'll let you know how it turns out.

Appreciatively,
-Greg
"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:uT**************@TK2MSFTNGP11.phx.gbl...
Hopefully explaining what this() and base() mean will clarify what's happening. When you have 2 constructors like the following:

public B() { ... }
public B(Token tkn) : this() { ... }

what that means is the second constructor will first invoke the zero-argument constructor for the class and then execute it's own code. Thus, if they were

public B() { this.a = 1; }
public B(Token tkn) : this() { this.b = 2; }

the second constructor is equivalent to:

public B(Token tkn) { this.a = 1; this.b = 2; }

Putting the repeated code in the first contructor promotes code reuse.

Now, whenever you construct a class that's a derived class, a constructor on the base class MUST be invoked. By default, it invokes the zero-argument contructor, assuming it's defined and not private. However, your derived class can cause a different constructor on the base class to be called by using the base() syntax. For example, in the class C, you have

public C(Token tkn) : base(tkn)

That tells the runtime to invoke the base class contructor that takes a Token argument (i.e. B(Token tkn)). Also, keep in mind that the base class constructor is always called BEFORE the derived class's constructor.

So, the full path of your constructor when you invoke "new D(tkn)" is:

A();
B();
C();
D(Token);

However, if you change your definition of the second constructor for D to be:

public D(Token tkn) : base(tkn)

then your constructor sequence would be:

A();
B();
B(Token);
C(Token);
D(Token);

which is probably more what you're looking for.

Ken
"hazz" <ha**@sonic.net> wrote in message news:uu**************@TK2MSFTNGP10.phx.gbl...

The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain.
I am calling the class at the bottom, "public class D" from a client app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------
I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #3

P: n/a
Worked beautifully Ken. After making sure the dependencies on the various projects in my vs.net solution were pointing to the right places, individual components were rebuilt and the whole solution integrated together with a total solution rebuild, I am doing exactly what I want to now in traversing the object hierarchy vis a vis the overloaded ctors.
Your absolutely excellent development of the problem and solution gave me such a good fundamental understanding that I didn't give up on early attempts when things weren't working. The objects through which the execution path traverses where in different projects/components/namespaces and once those where all integrated, it was such a joy to step through the code EXACTLY the way I had envisioned it. Only because of your assistance and help.

Thank you so much for taking the time.

Appreciatively,
-Greg Hazzard

"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:uT**************@TK2MSFTNGP11.phx.gbl...
Hopefully explaining what this() and base() mean will clarify what's happening. When you have 2 constructors like the following:

public B() { ... }
public B(Token tkn) : this() { ... }

what that means is the second constructor will first invoke the zero-argument constructor for the class and then execute it's own code. Thus, if they were

public B() { this.a = 1; }
public B(Token tkn) : this() { this.b = 2; }

the second constructor is equivalent to:

public B(Token tkn) { this.a = 1; this.b = 2; }

Putting the repeated code in the first contructor promotes code reuse.

Now, whenever you construct a class that's a derived class, a constructor on the base class MUST be invoked. By default, it invokes the zero-argument contructor, assuming it's defined and not private. However, your derived class can cause a different constructor on the base class to be called by using the base() syntax. For example, in the class C, you have

public C(Token tkn) : base(tkn)

That tells the runtime to invoke the base class contructor that takes a Token argument (i.e. B(Token tkn)). Also, keep in mind that the base class constructor is always called BEFORE the derived class's constructor.

So, the full path of your constructor when you invoke "new D(tkn)" is:

A();
B();
C();
D(Token);

However, if you change your definition of the second constructor for D to be:

public D(Token tkn) : base(tkn)

then your constructor sequence would be:

A();
B();
B(Token);
C(Token);
D(Token);

which is probably more what you're looking for.

Ken
"hazz" <ha**@sonic.net> wrote in message news:uu**************@TK2MSFTNGP10.phx.gbl...

The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain.
I am calling the class at the bottom, "public class D" from a client app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------
I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.