C# intialization of member variable trouble

M

mijalko

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?
 
B

Brian Gideon

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?

Hi,

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

Brian
 
O

Ollie Riches

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
 
C

Christof Nordiek

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
 
M

mijalko

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.


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




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 -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top