Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

c# - When stepping through my program the ToString() method is being called for no reason

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You probably have a watch on the form, or are in some other way displaying it's value in the debugger (through the "locals" window, the stack trace, etc.). The debugger uses ToString to display objects. If this is a problem in your program, you should likely re-design your ToString such that it isn't a problem for it to be called like this, or else simply avoiding using the debugger.

Having a very "expensive" ToString that you generally wish to avoid using is a situation to be wary of. There can be occasional exceptions where it does make sense, but callers generally expect it to be a cheap operation. Consider whether or not it would be appropriate to have some other method/property representing a display string that is more involved, leaving a simpler/cheaper ToString implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...