Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi, can someone possibly explain why this works? It's a portion of an article from here written in c#, I stripped out most of it to get to this. What I would like to know is how does the variable binaryNumber end up with the string 1111? I have tried to trace it but it never seems to call the overiden method ToString(). If you comment out the overiden ToString() method it doesn't work? How or when does it call that method? and why is the value 1111 in the binaryNumber and the number 15 in the field member m_value. Thanks.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  public struct Bin32
  {
    private int m_Value;

    private Bin32(int value)
    {
      m_Value = value;
    }

    public static implicit operator Bin32(int value)
    {
      return new Bin32(value);
    }

    public override string ToString()
    {
      return Convert.ToString(m_Value, 2);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {

      Bin32 binaryNumber = 15;
    }
  }
}
Posted
Updated 19-Nov-12 6:03am
v2
Comments
quintas_arias 19-Nov-12 12:12pm    
Hi, maybe chack this again please? There is no console.writeline and the conversion takes place through the overiden method ToString() but when and why? Thanks.

The ToString method is called by Console.WriteLine. The override is called. What makes you think it is not ?
 
Share this answer
 
Comments
quintas_arias 16-Nov-12 12:55pm    
Hi, when I trace through the code with break points at all 3 methods it never goes to that method so how and when does binaryNumber get the value 1111? But the ToString() method never seems to be called, at least through the trace. The static method gets called first, then the private method from there because of the new keyword, and when it returns from is when binaryNumber = 1111 but the trace never goes to the ToString() method so I'm confused as to when it gets called. Also, if binaryNumber is an instance of Bin32, then how does it hold the value 1111?
quintas_arias 19-Nov-12 12:12pm    
Hi, I've made a modification or two. Maybe you can take another look and explain it? Thanks.
You'll see the debugger display binaryNumber as {1111} which is, of course, the binary representation of 15. (The debugger cannot stop at a breakpoint set in ToString() or else the debugger itself could lock up.)
I just took your code and executed it.
The line in Main initializes binaryNumber with the value 15, which is stored in m_Value by the private constructor.
The debugger shows binaryNumber as {1111} as I noted above.
I added a Console.WriteLine(binaryNumber); into Main, right after the initialization, and it does call the ToString() and stop at the breakpoint there.
 
Share this answer
 
When your using console write line depending on the type of the variable you pass in you could get diferent outputs(change your data type before you use the console to write out) and also most data types have some implicit conversion look at the table below.

http://msdn.microsoft.com/en-US/library/y5b434w4(v=vs.80).aspx[^]

Use a format string:-

http://msdn.microsoft.com/en-US/library/s8s7t687(v=vs.80).aspx[^]
 
Share this answer
 
Comments
quintas_arias 16-Nov-12 13:03pm    
Hi, see comment above. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900