Reputation: 3318
So im trying to make a textbox ( well not exactly a textbox, just changing a spriteFont text ) on XNA 4.0 win game heres my code so far :
usernameVar.Draw(spriteBatch, newInputText);
That will draw newInputText string each frame
newInputText = username.update(mouse);
That will set the string but heres my problem
class Textbox
{
public Texture2D texture;
public Rectangle rectangle;
public bool isClicked;
public Textbox(Texture2D newTexture, Rectangle newRectangle)
{
texture = newTexture;
rectangle = newRectangle;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Color.White);
}
public String update(MouseState mouse)
{
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
var textboxText = new newText();
if (mouseRectangle.Intersects(rectangle))
{
isClicked = true;
if (isClicked)
{
textboxText.newtext = "a";
Connection.sendPacketBool("ae", textboxText.newtext);
return textboxText.newtext;
}
}
return null;
}
}
class newText
{
public String newtext
{
get
{
return this.newtext;
}
set
{
this.newtext = value;
}
}
}
This textbox.cs file is giving me some errors first what should I do to avoid returning something outside the IF statement ?
public String update(MouseState mouse)
{
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
var textboxText = new newText();
if (mouseRectangle.Intersects(rectangle))
{
isClicked = true;
if (isClicked)
{
textboxText.newtext = "a";
Connection.sendPacketBool("ae", "a");
return "yo";
}
}
return null;
}
Since that return null breaks me textbox ( i cant add null text to the spritefont ) Also If I remove the return null adding return "something" I get this error on the set propertie
An unhandled exception of type 'System.StackOverflowException'
Sorry for this, im very new to C# and all this stuff, thanks
Upvotes: 0
Views: 437
Reputation: 14153
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...
}
}
}
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 StackOverflowException
.
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
Here is an example:
private string newText; //Storage Field
public string Newtext //Accessor
{
get { return newText; }
set { newText = value; }
}
C# 3.0 has automatic properties, so this is not necessarily necessary (:P).
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.
Upvotes: 3