out on a method


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Gets the current category and stage.
  3. /// </summary>
  4. public bool GetCurrentCategoryAndStage(
  5. out Category category,
  6. out Stage stage)
  7. {
  8. category = null;
  9. stage = null;
  10. var result = Request.QueryString[WebConstants.CatagoryNavigatorQueryStringKey];
  11.  
  12. // When no options are specified use the defaults
  13. // which are the first category and first stage
  14. if (String.IsNullOrEmpty(result))
  15. {
  16. category = (ReferenceData.Categories != null && ReferenceData.Categories.Length > 0)
  17. ? ReferenceData.Categories[0] : null;
  18. stage = (category != null && category.Stages != null && category.Stages.Length > 0)
  19. ? category.Stages[0] : null;
  20.  
  21. // Success when all values have been set
  22. return category != null && stage != null;
  23. }
  24.  
  25. var options = result.Split(new[] { WebConstants.CategoryNavigatorSeparator }, StringSplitOptions.RemoveEmptyEntries);
  26.  
  27. // Format of querystring value should be {0}:{1} or to be more accurate
  28. // {0}[Separator]{1}
  29. if (options.Length < 2)
  30. return false;
  31.  
  32. // Here are the Id in hash form
  33. int categoryIdQueryStringEntry;
  34. int stageIdQueryStringEntry;
  35.  
  36. if (!int.TryParse(options[0], out categoryIdQueryStringEntry) || !int.TryParse(options[1], out stageIdQueryStringEntry))
  37. return false;
  38.  
  39. // Now there are all the required elements to get
  40. // the category and stage its time to return them
  41. // to the client
  42. category = ReferenceData.Categories
  43. .Where(x => ConvertToQueryStringEntry(x.Id) == categoryIdQueryStringEntry)
  44. .FirstOrDefault();
  45.  
  46. if (category == null)
  47. return false;
  48.  
  49. stage = category.Stages
  50. .Where(x => ConvertToQueryStringEntry(x.Id) == stageIdQueryStringEntry)
  51. .FirstOrDefault();
  52.  
  53. if(stage == null)
  54. return false;
  55.  
  56. return true;
  57. }out on a method

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.