asp.net - Trimming Strings within C# .Net application -
new enough c#.
just can't head around trimming string. want able trim whitespace after string messing database amount of whitespace get. here's 1 of entities i'd in.
[regularexpression(@"^[a-za-z0-9 ]+$", errormessage = "use letters , numbers please")] [display(name = "serial number ")] public string serialno { { try { string serialnumber = serialno.trim(); return serialnumber; } catch (exception e) { console.writeline(e); return serialno; } } set { this.serialno = serialno; } }
i don't know if i'm going wrong appreciated!
edit: error "an unhandled exception of type 'system.stackoverflowexception' occurred in photocopierinformationmanagementservices.dll"
the following end in stackoverflow excption keep executing infinitely, set block getting called again , due this.serialno=
inside set
block:
set { this.serialno = serialno; // calling set of serialno recursively }
you can change code :
private string _serialno; [regularexpression(@"^[a-za-z0-9 ]+$", errormessage = "use letters , numbers please")] [display(name = "serial number ")] public string serialno { { try { _serialno= _serialno.trim(); return _serialno; } catch (exception e) { console.writeline(e); return _serialno; } } set { _serialno= value; } }
but recommend trim in setter property like:
private string _serialno; [regularexpression(@"^[a-za-z0-9 ]+$", errormessage = "use letters , numbers please")] [display(name = "serial number ")] public string serialno { { return _serialno; } set { _serialno= value?.trim(); } }
Comments
Post a Comment