{"record":{"id":"bac2707a6523a50a","repo":"coding-horror/basic-computer-games","slug":"displaytext-must-be-greater-than-zero","errorCode":null,"errorMessage":"{DisplayText} must be greater than zero","messagePattern":"(.+?) must be greater than zero","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"06_Banner/csharp/banner.cs","lineNumber":83,"sourceCode":"        };\n\n\n        /// <summary>\n        /// This displays the provided text on the screen and then waits for the user\n        /// to enter a integer value greater than 0.\n        /// </summary>\n        /// <param name=\"DisplayText\">Text to display on the screen asking for the input</param>\n        /// <returns>The integer value entered by the user</returns>\n        private int GetNumber(string DisplayText)\n        {\n            Console.Write(DisplayText);\n            string TempStr = Console.ReadLine();\n\n            Int32.TryParse(TempStr, out int TempInt);\n\n            if (TempInt <= 0)\n            {\n                throw new ArgumentException($\"{DisplayText} must be greater than zero\");\n            }\n\n            return TempInt;\n        }\n\n        /// <summary>\n        /// This displays the provided text on the screen and then waits for the user\n        /// to enter a Y or N.  It cheats by just looking for a 'y' and returning that\n        /// as true.  Anything else that the user enters is returned as false.\n        /// </summary>\n        /// <param name=\"DisplayText\">Text to display on the screen asking for the input</param>\n        /// <returns>Returns true or false</returns>\n        private bool GetBool(string DisplayText)\n        {\n            Console.Write(DisplayText);\n            return (Console.ReadLine().StartsWith(\"y\", StringComparison.InvariantCultureIgnoreCase));\n        }\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/06_Banner/csharp/banner.cs#L65-L101","documentation":"Thrown as ArgumentException by GetNumber() in the Banner program when the parsed integer is <= 0. Int32.TryParse writes 0 into TempInt on failure (non-numeric or null input), so both genuinely non-positive input and unparseable garbage trigger it. GetNumber feeds Horizontal/Vertical scaling factors for the banner print, which must be positive.","triggerScenarios":"User pressing Enter without typing (null/empty -> TryParse fails -> TempInt=0); typing 0, a negative number, or letters like 'abc' at the 'Horizontal ' or 'Vertical ' prompts in GetInput() (banner.cs:119-120).","commonSituations":"Empty input at the dimension prompt; a user typing a negative dimension; pasting non-numeric text; running the program with redirected empty stdin.","solutions":["Enter a positive integer (1 or greater) at the Horizontal/Vertical prompts.","If automating, pipe a positive integer into stdin for those prompts.","Make GetNumber loop and re-prompt on <= 0 instead of throwing, for a friendlier UX."],"exampleFix":"// before\nInt32.TryParse(TempStr, out int TempInt);\nif (TempInt <= 0) {\n    throw new ArgumentException($\"{DisplayText} must be greater than zero\");\n}\nreturn TempInt;\n\n// after: re-prompt instead of throwing\nint TempInt;\nwhile (!Int32.TryParse(Console.ReadLine(), out TempInt) || TempInt <= 0) {\n    Console.Write($\"{DisplayText} must be a positive integer. Try again: \");\n}\nreturn TempInt;","handlingStrategy":"validation","validationCode":"// C#: validate before relying on TryParse result\nint GetNumber(string prompt) {\n  int value;\n  while (!Int32.TryParse(Console.ReadLine(), out value) || value <= 0) {\n    Console.Write($\"{prompt} must be a positive integer: \");\n  }\n  return value;\n}","typeGuard":null,"tryCatchPattern":"// In GetInput(), catch and re-prompt instead of letting it propagate\ntry { Horizontal = GetNumber(\"Horizontal \"); }\ncatch (ArgumentException) { /* re-ask or default */ }","preventionTips":["Never assume TryParse succeeded; check its boolean return.","Loop on invalid input rather than throwing for user-entry errors.","When automating, feed positive integers for every GetNumber call."],"tags":["csharp","input-validation","user-input","argument"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}