c# - XNA Textbox loop -
so im trying make textbox ( not textbox, changing spritefont text ) on xna 4.0 win game heres code far :
usernamevar.draw(spritebatch, newinputtext);
that draw newinputtext string each frame
newinputtext = username.update(mouse);
that set string heres 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 { { return this.newtext; } set { this.newtext = value; } } }
this textbox.cs file giving me errors first should avoid returning outside 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 return null breaks me textbox ( cant add null text spritefont ) if remove return null adding return "something" error on set propertie
an unhandled exception of type 'system.stackoverflowexception'
sorry this, im new c# , stuff, thanks
i'm not sure of exact structure of project, , i'm not sure reason newtext
class, property contains called itself, over, , over, , on again.
class newtext { public string newtext { { return this.newtext; //get newtext again, gets again, , again, etc } set { this.newtext = value; //set newtext, calls set again, , again... } } }
when or set newtext
, or set on , on again, resulting in recursive loop. never end , lead stackoverflowexception
.
the proper way use property have public accessor (newtext
) perform logic (in case, , set) , return or set value, in case, storage variable newtext
here example:
private string newtext; //storage field public string newtext //accessor { { return newtext; } set { newtext = value; } }
c# 3.0 has automatic properties, not necessary (:p).
as added note, not have use string
class, string
, string
same thing, using string
preferred method.
Comments
Post a Comment