{"id":335,"date":"2018-10-10T08:55:16","date_gmt":"2018-10-10T08:55:16","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=335"},"modified":"2026-03-25T11:25:26","modified_gmt":"2026-03-25T11:25:26","slug":"using-strings-validate","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/strings\/using-strings-validate","title":{"rendered":"Using Strings-Validate"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Verify Numeric Value<\/h4>\n\n\n\n<p>Convert string to value without erroring on fail<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Try and convert without checking result\n\tint.TryParse(MyStringValue, out Value);\t\t\/\/Value will be set to 0 if conversion fails\n\n\t\/\/Convert and get result if it worked or not\n\tif (int.TryParse(MyStringValue, out Value))\t\/\/Returns true if conversion sucessful\n\t{\n\t\t\/\/Conversion was sucessful\n\t}<\/code><\/pre>\n\n\n\n<p>For larger values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    UInt64 Value = 123;\n    ValueIsNumeric = UInt64.TryParse(MyStringValue, out Value);\t\t\/\/Value will be set to 0 if conversion fails<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Verifying a form TextBox value example<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    \/\/*****************************************************\n    \/\/*****************************************************\n    \/\/*********** VERIFY NUMBERIC TEXT BOX VALUE **********\n    \/\/*****************************************************\n    \/\/*****************************************************\n    private void VerifyTextBoxNumericValue(ref TextBox TextBox1, int MinValue, int MaxValue, int DefaultValue)\n    {\n        try\n        {\n            int Value = 0;\n            if (!(int.TryParse(TextBox1.Text, out Value)))\n            {\n                TextBox1.Text = Convert.ToString(DefaultValue);\n            }\n\n            if (Value &lt; MinValue)\n                TextBox1.Text = Convert.ToString(MinValue);\n            if (Value &gt; MaxValue)\n                TextBox1.Text = Convert.ToString(MaxValue);\n        }\n        catch (Exception)\n        {\n        }\n    }\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n    VerifyTextBoxNumericValue(ref MyTextBox, 0, 256, 256);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Verify a byte array contains only ASCII characters<\/h4>\n\n\n\n<p>Skips the first 5 bytes, tests the rest<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\t\t\t\t\tbool IsAscii = RxData.Skip(5).Take(Rs485FeedReceivedPacketLength).All(b => (b >= ' ' &amp;&amp; b &lt;= '~') || b == '\\r' || b == '\\n');\t\/\/Check the data is all ascii characters\n\t\t\t\t\t\tif (!IsAscii)\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\tsTemp = \"\";\n\t\t\t\t\t\tsTemp = System.Text.Encoding.UTF8.GetString(RxData, 5, Rs485FeedReceivedPacketLength);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Verify Numeric Value Convert string to value without erroring on fail For larger values: Verifying a form TextBox value example Verify a byte array contains only ASCII characters Skips the first 5 bytes, tests the rest<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":1426,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/335\/revisions\/1426"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}