asp.net - C# is there a nicer way of writing this? -


int uploadsid; int pagenumber; int x; int y; int w; int h;  bool isvaliduploadid = int.tryparse(context.request.querystring["uploadid"], out uploadsid); bool isvalidpage = int.tryparse(context.request.querystring["page"], out pagenumber); bool isvalidx = int.tryparse(context.request.querystring["x"], out x); bool isvalidy = int.tryparse(context.request.querystring["y"], out y); bool isvalidw = int.tryparse(context.request.querystring["w"], out w); bool isvalidh = int.tryparse(context.request.querystring["h"], out h);  if (isvaliduploadid && isvalidpage && isvalidx && isvalidy & isvalidw & isvalidh) { 

this ajax handler, checking passed params ok. considered bad, , there better way write this, or not important?

assuming you're not going use individual bool variables elsewhere, could write as:

int uploadsid, pagenumber, x, y, w, h; if (int.tryparse(context.request.querystring["uploadid"], out uploadsid) &&     int.tryparse(context.request.querystring["page"], out pagenumber) &&     int.tryparse(context.request.querystring["x"], out x) &&     int.tryparse(context.request.querystring["y"], out y) &&     int.tryparse(context.request.querystring["w"], out w) &&     int.tryparse(context.request.querystring["h"], out h)) { } 

you may want extract out int.tryparse(context.request.querystring[name], out variable separate method, leaving like:

int uploadsid, pagenumber, x, y, w, h; if (tryparsecontextint32("uploadid", out uploadsid) &&     tryparsecontextint32("page", out pagenumber) &&     tryparsecontextint32("x", out x) &&     tryparsecontextint32("y", out y) &&     tryparsecontextint32("w", out w) &&     tryparsecontextint32("h", out h)) { } 

alternatively, encapsulate context data new type tryparse method, you'd have like:

pagedetails details; if (pagedetails.tryparse(context.request.querystring)) {     // access details.page, details.uploadid etc } 

that's more work, think make code cleaner.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -