CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: code qustion

  1. #1
    Join Date
    Sep 2009
    Posts
    4

    code qustion

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

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: code qustion

    Please use code tags to make your snippet readable.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: code qustion

    "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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Sep 2009
    Posts
    4

    Re: code qustion

    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().

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: code qustion

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Sep 2009
    Posts
    4

    Re: code qustion

    Oh I see now! "the debugger calling" escapes my attention. Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured