且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XNA 文本框循环

更新时间:2023-11-03 08:28:34

我不确定您项目的确切结构,也不确定 newText 类的原因,但是它包含的属性会一遍又一遍地调用自己.

I'm not sure of the exact structure of your project, and I'm not sure the reason for the newText class, but the property that it contains called itself, over, and over, and over again.

class newText
{
    public String newtext
    {
        get 
        { 
            return this.newtext; //Get newtext again, which gets it again, and again, etc
        }
        set 
        { 
            this.newtext = value; //Set newtext, which calls set again, and again...
        }
    }
}

当您获取或设置 newtext 时,它会一遍又一遍地获取或设置自身,从而导致递归循环.这永远不会结束,并会导致 ***Exception.

When you get or set newtext, it will get or set itself over and over again, resulting in a recursive loop. This will never end and will lead to a ***Exception.

使用属性的正确方法是使用公共访问器(NewText) 将执行逻辑(在这种情况下,只是获取和设置)并返回或设置一个值,在这种情况下,存储变量 newText

The proper way to use a property is to have the public accessor (NewText) that will perform logic (in this case, just get and set) and return or set a value, in this case, the storage variable newText

这是一个例子:

private string newText; //Storage Field

public string Newtext //Accessor
{
    get { return newText; }
    set { newText = value; }
}

C# 3.0 具有自动属性,所以这不一定必要的 (:P).

C# 3.0 has automatic properties, so this is not necessarily necessary (:P).

作为补充说明,您不必使用 String 类,stringString同样的事情,但使用string通常是首选方法.

As an added note, you do not have to use the String class, string and String are the same thing, but using string is usually the preferred method.