473,386 Members | 1,821 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,386 software developers and data experts.

Some construction that I don't understand why it works as it does.

Hello!

I have this small class CustomException with one ctor see below.
In this class I have both override the ToString and Message.

Now to my question if I made this call
string ss = ToString(); in the ctor for this class CustomException
I call the overridden ToString in this class CustomException.

If I made this call string sss = base.ToString();
in the ctor for this class CustomException I also call the overridden
ToString in this class CustomException

Now can somebody explain why second example will call the ToString in my
class CustomException
If I write little pseudocode I can understand it if we had this
construction.
I just use base as an instance variable of CustomException
Exception base = new CustomException()
base.ToString();
because here the dynamic type is CustomException and we will have polymorf
call so ToString in class CustomException will be called.

But when we have string sss = base.ToString();
what is the dynamic type for base?
public class CustomException : Exception
{
private MathClass mathobject;
private string sMessage;

public CustomException(string sMsg, MathClass mo)
{
mathobject = mo;
sMessage = sMsg;

//string ss = ToString();
//string sss = base.ToString();
}

override public string Message
{
get{return String.Format("Message is <{0}>, Object is {1}",
sMessage, mathobject.ToString());}
}

override public string ToString()
{
string s = Message;
s += "\nException thrown from ";
s += TargetSite;
return s;
}
}

//Tony


Apr 26 '06 #1
1 1051
Tony Johansson <jo*****************@telia.com> wrote:
Hello!

I have this small class CustomException with one ctor see below.
In this class I have both override the ToString and Message.

Now to my question if I made this call
string ss = ToString(); in the ctor for this class CustomException
I call the overridden ToString in this class CustomException.

If I made this call string sss = base.ToString();
in the ctor for this class CustomException I also call the overridden
ToString in this class CustomException

Now can somebody explain why second example will call the ToString in my
class CustomException
If I write little pseudocode I can understand it if we had this
construction.
I just use base as an instance variable of CustomException
Exception base = new CustomException()
base.ToString();
because here the dynamic type is CustomException and we will have polymorf
call so ToString in class CustomException will be called.

But when we have string sss = base.ToString();
what is the dynamic type for base?


When you use base.Whatever() you aren't making a virtual call. The
method implementation in the base class is *always* used. So, that will
call Exception.ToString().

Note that calling virtual methods in a constructor is generally a bad
idea for precisely this kind of reason.

Personally I would favour formatting the message in the constructor and
passing it up to the base constructor, without overriding anything.
That avoids many of the pitfalls of inheritance. See

http://msmvps.com/blogs/jon.skeet/ar...ritancetax.asp
x

for more on that topic.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
15
by: Victor Bazarov | last post by:
Hello, Take a look at this program: ----------------------------------- class B { B(const B&); B& operator=(const B&); public: B(int);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.