This is a bizarre problem I'm having even my senior programmer next to me is also confused. The issue in full is that somehow my ToString()
method is being called and I don't know or understand how this is my code
static void Main(string[] args)
{
Console.Out.WriteLine("Blank Constructor");
Form form = new Form(); <-- ToString() gets called on this line.
form.ToString();
Console.Read();
}
public Form()
{
FormName = "";
FormImageLocation = "";
FormDescription = "";
FormID = 0;
CreatedDate = DateTime.Now;
LastUpdate = DateTime.Now;
Fields = new List<Field>();
Packets = new List<Packet>(); <-- This line in the constructor
}
public override string ToString()
{
string returnString;
returnString = " Form Name: " + FormName + " Form Image Location: " + FormImageLocation + "Form Description: " + FormDescription + " FormID: " + FormID + " Created Date: " + CreatedDate + " LastUpdate: " + LastUpdate ;
if (fields.Count != 0)
{
foreach (var field in fields)
{
returnString += field.ToString();
}
}
else
{
returnString += "!!! This Form has no Fields !!!";
}
if (Packets.Count != 0)
{
foreach (var packet in Packets)
{
returnString += packet.ToString();
}
}
else
{
returnString += " !!! This Form does not belong to any Packets !!!";
}
Console.Out.WriteLine(returnString);
return returnString;
}
public Packet(string packet_name, List<Form> list_of_forms)
{
PacketName = packet_name;
forms = list_of_forms;
}
This seemingly random recurrence of the ToString()
printing ONLY occurs when i step through the program. it will print on the line I designated above and also when the constructor exits and prints like crazy as I'm stepping through the ToString()
method itself. I placed a break point in the ToString()
but it will only stop on the breakpoint when the ToString()
is legitimetly called, so to be clear when i step through and it does this random printing it will not stop at the breakpoints within the ToString()
. I went through and removed all calls to the ToString()
and it still get randomly called, when i commented out the returnString
variable and just returned "her there" the problem went away but that doesn't help anything. if i just run the program without breakpoints this problem does not occur. some of you may say that if it works when running it doesn't matter but it makes me extremely wary that if i run into a code issue down the road and i try to step through the code to find the problem i will get different results and hinder the debugging. I tried cover the whole issue and what i have tried and provide all code needed, if i was unclear about something let me know and i will try to explain it again. lastly I am on a Windows 7 64 bit machine and I am using Visual Studio C# 2010 Express.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…