Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

C# intialization of member variable trouble


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?
On Feb 6, 7:48 am, mija@gmail.com wrote:

Hi,

Well, test.ToString() isn't even called so why would you expect to see
it in the debugger?

Brian

-----------------------------------------------Reply-----------------------------------------------

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 the
variable implementation.

HTH

Ollie Riches

<mija@gmail.com> wrote in message

news:1170769701.242518.251950@q2g2000cwa.googlegroups.com...

Hi mijalko,

not sure, if this is your problem, but you should avoid to call the
ToString() from the watch window, at least, if you try to debug the
initialization/caching behaviour of your class. Instead, put strTest into
the watchwindow.

hth
Christof

<mija@gmail.com> schrieb im Newsbeitrag
news:1170769701.242518.251950@q2g2000cwa.googlegroups.com...

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:

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc