Click to See Complete Forum and Search --> : code qustion


szita1
November 26th, 2009, 04:31 AM
I was studying this code below and i dont understand sg:
i run the code with the debugger and when the r1 is created and its constructor is stepped out,
in the "locals" window, "this" object already holds the value {[/B][9]: IX} as if the whole code has finished runnig. How the value can be there when the code which should give its value hasnt run yet.

// demonstrate operator overloading
namespace OperatorOverloading
{
// class to hold Roman numerals
public class Roman
{
private int val1; // the actual value of the numeral

// constructor takes an int
// creates a numeral
public Roman(int val)
{
Console.WriteLine("In Roman Constructor(int)");
this.val1 = val;
}

// override ToString to display the roman numeral
// strategy: handle thousands (M), five hundreds (D)
// hundreds (C), Fifties (L), Tens (X) and ones (I)
// special case for 900, 90, 9 and 400, 40 and 4
public override string ToString()
{
// comments show how 3899 would be converted 3899
int numThousands = val1 / 1000; // 3
int remainder = val1 % 1000; // 899
int numNineHundreds = remainder / 900; // 0
remainder %= 900; // 899
int numFiveHundreds = remainder / 500; // 1
remainder %= 500; // 399
int numHundreds = remainder / 100; // 3
remainder %= 100; // 99
int numNineties = remainder / 90; // 1
remainder %= 90; // 9
int numFifties = remainder / 50; // 0
remainder %= 50; // 9
int numTens = remainder / 10; // 0
remainder %= 10; // 9
int numNines = remainder / 9; // 1
remainder %= 9; // 0
int numFives = remainder / 5; // 0
int numOnes = remainder % 5; // 0

string output = "[" + val1 + "]: ";
StringBuilder s = new StringBuilder(output);
for (int i = 0; i < numThousands; i++)
s.Append( 'M' );
if (numNineHundreds == 1)
s.Append("CM");
if (numFiveHundreds == 1)
s.Append( 'D' );
if (numHundreds == 4)
s.Append( "CD" );
else
for(int i = 0; i < numHundreds; i++)
s.Append( 'C' );
if (numNineties == 1)
s.Append("XC");
if (numFifties == 1)
s.Append( 'L' );
if (numTens == 4)
s.Append( "XL" );
else
for(int i = 0; i < numTens; i++)
s.Append( 'X' );
if (numNines == 1)
s.Append("IX");
if (numFives == 1)
s.Append( 'V' );
if (numOnes == 4)
s.Append( "IV" );
else
for (int i = 0; i < numOnes; i++)
s.Append( 'I' );

return s.ToString();
}
}


public class Tester
{
public void Run()
{
Console.WriteLine("\n creating new Roman(9)");
Roman r1 = new Roman(9);
Console.WriteLine("r1: {0}", r1.ToString());
}
static void Main()
{
Tester t = new Tester();
t.Run();
Console.ReadLine();
}
}
}

boudino
November 26th, 2009, 06:06 AM
Please use code tags (http://www.codeguru.com/forum/misc.php?do=bbcode#code) to make your snippet readable.

darwen
November 26th, 2009, 07:37 AM
"this" object already holds the value


When in the debugger, the value shown for a class is what is returned by its ToString() function.

That's why you're seeing a valid value - the debugger is calling 'ToString()' which dynamically builds up the string.

Darwen.

szita1
November 26th, 2009, 01:13 PM
Thanks for the answer, but i dont see when ToString() is invoked. All i'm seeing is that when the constructor finished, the value shown (in the 'Locals' window/'Value' column and 'this' line ) is already there without invoking ToString().

darwen
November 26th, 2009, 01:26 PM
the value shown (in the 'Locals' window/'Value' column and 'this' line ) is already there without invoking ToString().


And where do you think the locals window gets the value to put on screen ?

By calling the ToString() method of the class.

I'll say it again - it's the debugger calling ToString().

Darwen.

szita1
November 26th, 2009, 01:42 PM
Oh I see now! "the debugger calling" escapes my attention. Thanks a lot.