{"id":12101,"date":"2023-03-20T06:10:34","date_gmt":"2023-03-20T05:10:34","guid":{"rendered":"https:\/\/www.q-software-solutions.de\/blog\/?p=12101"},"modified":"2023-03-20T06:10:37","modified_gmt":"2023-03-20T05:10:37","slug":"thats-a-decent-suggestion","status":"publish","type":"post","link":"https:\/\/www.q-software-solutions.de\/blog\/2023\/03\/thats-a-decent-suggestion\/","title":{"rendered":"That&#8217;s a decent suggestion"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\nPROCEDURE assertNoDuplicatesInTableRow(tblControl is Control,  cbColName is Control, vOldValue is Variant, sDialogText is string )\n\nnFoundIndex\t\t\t\t\tis int\nnRetValDialog, nRetValInput\tare int\nvSearchValue, vNewValue\t\tis string\nvSearchValue\t= cbColName\n\nnFoundIndex\t\t= TableSearch(cbColName, vSearchValue)\n\nWHILE ((nFoundIndex > 0) AND  (nFoundIndex &lt;> tblControl))\n\tnRetValDialog =  Dialog(sDialogText, &#91;\"Change\", \"Cancel\"], 1, 2)\n\tIF (nRetValDialog = 1) THEN\n\t\tnRetValInput = Input(\"New value\", vNewValue)\n\t\tIF nRetValInput = 2 THEN\n\t\t\tMySelf = vOldValue\n\t\t\tBREAK\n\t\tELSE\n\t\t\tnFoundIndex = TableSearch(cbColName, vNewValue)\n\t\t\tIF nFoundIndex = tblControl THEN\n\t\t\t\tMySelf = vNewValue\t\t\t\t\n\t\t\t\tBREAK\n\t\t\tEND\n\t\tEND\n\tELSE\n\t\tMySelf = vOldValue\n\t\tBREAK\n\tEND\t\t\nEND\n\n\n\n<\/code><\/pre>\n\n\n\n<p>Asked ChatGPT to refactor ist:<br>Answer:  Get rid of the loop and recall recursivly. <br><br>IF nFoundIndex > 0 AND nFoundIndex &lt;> tblControl THEN Dialog(&#8222;Duplicate value found. Please enter a different value.&#8220;, [&#8222;OK&#8220;], 1, 1) assertNoDuplicatesInTableRow(tblControl, cbColName, vOldValue, sDialogText) ELSE MySelf = vNewValue END<br><br>That of course not WLanguage bu thte idea is decent:<br>One can write it this way:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nPROCEDURE assertNoDuplicatesInTableRow(tblControl is Control,  cbColName is Control, vOldValue is Variant, sDialogText is string )\n\nnFoundIndex\t\t\t\t\tis int\nnRetValDialog, nRetValInput\tare int\nvSearchValue, vNewValue\t\tis Variant\nvSearchValue\t= cbColName\n\nnFoundIndex\t\t= TableSearch(cbColName, cbColName)\n\nIF ((nFoundIndex > 0) AND  (nFoundIndex &lt;> tblControl)) THEN\n\tnRetValDialog = Dialog(sDialogText, &#91;\"Change\", \"Cancel\"], 1, 2)\n\tIF nRetValDialog = 2 THEN\n\t\t\tMySelf = vOldValue\n\tELSE\n\t\tnRetValInput = Input(\"New value\", vNewValue)\n\t\tIF nRetValInput = 2 THEN\n\t\t\tMySelf = vOldValue\n\t\t\t\/\/ BREAK\n\t\tELSE\n\t\t\tcbColName.Value = vNewValue\n\t\t\tassertNoDuplicatesInTableRow(tblControl,cbColName, vOldValue, sDialogText)\t\t\t\n\t\tEND\n\tEND\n\t\t\nEND\n\n\/\/WHILE ((nFoundIndex > 0) and  (nFoundIndex &lt;> tblControl))\n\/\/\tnRetValDialog =  Dialog(sDialogText, &#91;\"Change\", \"Cancel\"], 1, 2)\n\/\/\tIF (nRetValDialog = 1) THEN\n\/\/\t\tnRetValInput = Input(\"New value\", vNewValue)\n\/\/\t\tIF nRetValInput = 2 THEN\n\/\/\t\t\tMySelf = vOldValue\n\/\/\t\t\tBREAK\n\/\/\t\tELSE\n\/\/\t\t\tnFoundIndex = TableSearch(cbColName, vNewValue)\n\/\/\t\t\tIF nFoundIndex = tblControl THEN\n\/\/\t\t\t\tMySelf = vNewValue\t\t\t\t\n\/\/\t\t\t\tbreak\n\/\/\t\t\tEND\n\/\/\t\tEND\n\/\/\tELSE\n\/\/\t\tMySelf = vOldValue\n\/\/\t\tBREAK\n\/\/\tEND\t\t\n\/\/END\n\/\/\n\/\/\n\n<\/code><\/pre>\n\n\n\n<p>Is it better? Well, it avoid a loop with different places where one has to break it<br>It avoids  a double call to TableSearch, but replaces it with a recursive call<br><br>Other really good suggestions:<br><\/p>\n\n\n\n<p>function to avoid a double in a two dimensional array<\/p>\n\n\n\n<p>You can use a loop to iterate over the elements in the two-dimensional array and check if a given value already exists. Here&#8217;s an example implementation in C#:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public bool CheckIfValueExistsInArray(int[,] array, int value)\n{\n    for (int i = 0; i &lt; array.GetLength(0); i++)\n    {\n        for (int j = 0; j &lt; array.GetLength(1); j++)\n        {\n            if (array[i, j] == value)\n            {\n                return true;\n            }\n        }\n    }\n    return false;\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>CheckIfValueExistsInArray<\/code> method takes in a two-dimensional integer array and a value to check for. It then iterates over each element in the array using two nested loops, comparing each element to the given value. If a match is found, the method returns <code>true<\/code>. If the loop completes without finding a match, the method returns <code>false<\/code>.<\/p>\n\n\n\n<p>You can call this method to check if a value already exists in a two-dimensional array before inserting a new value. You can then decide whether or not to insert the new value based on the return value of the method.<br><br>And: <\/p>\n\n\n\n<p>How can I avoid the double loop in the suggested code?<\/p>\n\n\n\n<p>If you want to avoid using nested loops, you can use the <code>Array.IndexOf<\/code> method to search for the value in a flattened version of the two-dimensional array. Here&#8217;s an example implementation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">csharpCopy code<code>public bool CheckIfValueExistsInArray(int[,] array, int value)\n{\n    int[] flattenedArray = new int[array.GetLength(0) * array.GetLength(1)];\n    Buffer.BlockCopy(array, 0, flattenedArray, 0, flattenedArray.Length * sizeof(int));\n    return Array.IndexOf(flattenedArray, value) != -1;\n}\n<\/code><\/pre>\n\n\n\n<p>In this example, the <code>CheckIfValueExistsInArray<\/code> method takes in a two-dimensional integer array and a value to check for. It creates a new one-dimensional integer array with the same number of elements as the two-dimensional array by using <code>Buffer.BlockCopy<\/code> to copy the elements from the two-dimensional array to the new array. It then uses the <code>Array.IndexOf<\/code> method to search for the value in the flattened array. If the value is found, the method returns <code>true<\/code>. If the value is not found, the method returns <code>false<\/code>.<\/p>\n\n\n\n<p>Note that this approach has some overhead because of the need to create a new flattened array, so it may not be faster than using nested loops for very small arrays. However, it can be more concise and easier to read for larger arrays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Asked ChatGPT to refactor ist:Answer: Get rid of the loop and recall recursivly. IF nFoundIndex > 0 AND nFoundIndex &lt;> tblControl THEN Dialog(&#8222;Duplicate value found. Please enter a different value.&#8220;, [&#8222;OK&#8220;], 1, 1) assertNoDuplicatesInTableRow(tblControl, cbColName, vOldValue, sDialogText) ELSE MySelf = vNewValue END That of course not WLanguage bu thte idea is decent:One can write it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7758,7979],"tags":[],"class_list":["post-12101","post","type-post","status-publish","format-standard","hentry","category-it","category-programmierung"],"_links":{"self":[{"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/posts\/12101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/comments?post=12101"}],"version-history":[{"count":0,"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/posts\/12101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/media?parent=12101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/categories?post=12101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.q-software-solutions.de\/blog\/wp-json\/wp\/v2\/tags?post=12101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}