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();
}
}
}
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();
}
}
}