当我执行程序时,ToString()方法被无缘无故地调用 [英] When stepping through my program the ToString() method is being called for no reason

查看:140
本文介绍了当我执行程序时,ToString()方法被无缘无故地调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奇怪的问题,即使我旁边的高级程序员也感到困惑。完整的问题是,不知何故我的 ToString()方法正在被调用,我不知道或了解这是我的代码这样的代码

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;
}

这个看似随机的 ToString() / code>打印仅在我通过程序时发生。它将打印在我上面指定的行上,并且当构造函数退出并打印像疯狂,因为我正在通过 ToString()方法本身打印。我在 ToString()中放置了一个断点,但是当 ToString()是所以要清楚,当我通过,它做这个随机打印它不会停止在 ToString()中的断点。我通过并删除了对 ToString()的所有调用,并且仍然随机调用,当我注释掉 returnString 变量,刚刚返回她在那里,问题消失了,但没有任何帮助。如果我只是运行没有断点的程序,这个问题不会发生。有些人可能会说,如果它运行时工作没关系,但它使我非常警惕,如果我遇到一个代码问题的道路上,我尝试通过代码来找到问题,我会得到不同的结果并阻止调试。我尝试覆盖整个问题,我已经尝试过并提供所需的所有代码,如果我不清楚有什么让我知道,我会尝试再次解释。最后我在一台Windows 7 64位机器上,我使用的是Visual Studio C#2010 Express。

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.

推荐答案

你可能有一个手表或者以某种其他方式在调试器中显示它的值(通过本地窗口,堆栈跟踪等)。调试器使用 ToString 来显示对象。如果这是您的程序中的问题,您应该重新设计您的 ToString ,使其不是被调用的问题像这样,否则只是避免使用调试器。

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.

有一个非常昂贵的 ToString ,你一般希望避免使用的是一个需要警惕的情况。偶尔会有一些例外情况,但是呼叫者通常期望它是一个廉价的操作。考虑是否使用一些其他方法/属性代表更多参与的显示字符串是合适的,留下更简单/便宜的 ToString 实现。

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.

这篇关于当我执行程序时,ToString()方法被无缘无故地调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆