> Look at this code:
> class test
> {
> private bool bInitialized = false;
> private string strTest;
> public string str2 = "";
> private void Init()
> {
> if (bInitialized)
> return;
> bInitialized = true;
> strTest = str2;
> }
> public test()
> {
> strTest = "";
> bInitialized = false;
> }
> public string StrTest
> {
> get
> {
> Init();
> return strTest;
> }
> }
> public override string ToString()
> {
> Init();
> return strTest;
> }
> }
> class Program
> {
> static void Main(string[] args)
> {
> test t = new test();
> t.str2 = "AAAA";
> Console.WriteLine(t.StrTest);
> Console.ReadLine();
> }
> }
> Problem is in overriding ToString() method. Program will execute
> Init() from ToString() and you will not be able to see that in
> debuger. You can even change back bInitialized to false in constructor
> but in next step Init() from ToString() will be called and you will
> not see that in debuger. Why debuger does not show this call?
I found what was the problem. If you put breakpoint at
test t = new test();
and debug with step into, and watch variable t in "Autos" window,
environment will automaticly call ToString(). But this call you can
not stop with breakpoint in ToString method.
On Feb 6, 3:23 pm, "Ollie Riches" <ollie_ric@hotmail.com> wrote:
> that is because your program never calls your overridden implementation of
> ToString()
> your main method should be I believe if you want ToString called on the
> class test:
> test t = new test();
> t.str2 = "AAAA";
> Console.WriteLine(t);
> Console.ReadLine();
> Remember the ToString you are overiding is the class implementation NOT thevariableimplementation.
> HTH
> Ollie Riches
> <mija@gmail.com> wrote in message
> news:1170769701.242518.251950@q2g2000cwa.googlegroups.com...
> > Look at this code:
> > class test
> > {
> > private bool bInitialized = false;
> > private string strTest;
> > public string str2 = "";
> > private void Init()
> > {
> > if (bInitialized)
> > return;
> > bInitialized = true;
> > strTest = str2;
> > }
> > public test()
> > {
> > strTest = "";
> > bInitialized = false;
> > }
> > public string StrTest
> > {
> > get
> > {
> > Init();
> > return strTest;
> > }
> > }
> > public override string ToString()
> > {
> > Init();
> > return strTest;
> > }
> > }
> > class Program
> > {
> > static void Main(string[] args)
> > {
> > test t = new test();
> > t.str2 = "AAAA";
> > Console.WriteLine(t.StrTest);
> > Console.ReadLine();
> > }
> > }
> > Problem is in overriding ToString() method. Program will execute
> > Init() from ToString() and you will not be able to see that in
> > debuger. You can even change back bInitialized to false in constructor
> > but in next step Init() from ToString() will be called and you will
> > not see that in debuger. Why debuger does not show this call?- Hide quoted text -
>