PROCEDURE assertNoDuplicatesInTableRow(tblControl is Control, cbColName is Control, vOldValue is Variant, sDialogText is string )
nFoundIndex is int
nRetValDialog, nRetValInput are int
vSearchValue, vNewValue is string
vSearchValue = cbColName
nFoundIndex = TableSearch(cbColName, vSearchValue)
WHILE ((nFoundIndex > 0) AND (nFoundIndex <> tblControl))
nRetValDialog = Dialog(sDialogText, ["Change", "Cancel"], 1, 2)
IF (nRetValDialog = 1) THEN
nRetValInput = Input("New value", vNewValue)
IF nRetValInput = 2 THEN
MySelf = vOldValue
BREAK
ELSE
nFoundIndex = TableSearch(cbColName, vNewValue)
IF nFoundIndex = tblControl THEN
MySelf = vNewValue
BREAK
END
END
ELSE
MySelf = vOldValue
BREAK
END
END
Asked ChatGPT to refactor ist:
Answer: Get rid of the loop and recall recursivly.
IF nFoundIndex > 0 AND nFoundIndex <> tblControl THEN Dialog(„Duplicate value found. Please enter a different value.“, [„OK“], 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 this way:
PROCEDURE assertNoDuplicatesInTableRow(tblControl is Control, cbColName is Control, vOldValue is Variant, sDialogText is string )
nFoundIndex is int
nRetValDialog, nRetValInput are int
vSearchValue, vNewValue is Variant
vSearchValue = cbColName
nFoundIndex = TableSearch(cbColName, cbColName)
IF ((nFoundIndex > 0) AND (nFoundIndex <> tblControl)) THEN
nRetValDialog = Dialog(sDialogText, ["Change", "Cancel"], 1, 2)
IF nRetValDialog = 2 THEN
MySelf = vOldValue
ELSE
nRetValInput = Input("New value", vNewValue)
IF nRetValInput = 2 THEN
MySelf = vOldValue
// BREAK
ELSE
cbColName.Value = vNewValue
assertNoDuplicatesInTableRow(tblControl,cbColName, vOldValue, sDialogText)
END
END
END
//WHILE ((nFoundIndex > 0) and (nFoundIndex <> tblControl))
// nRetValDialog = Dialog(sDialogText, ["Change", "Cancel"], 1, 2)
// IF (nRetValDialog = 1) THEN
// nRetValInput = Input("New value", vNewValue)
// IF nRetValInput = 2 THEN
// MySelf = vOldValue
// BREAK
// ELSE
// nFoundIndex = TableSearch(cbColName, vNewValue)
// IF nFoundIndex = tblControl THEN
// MySelf = vNewValue
// break
// END
// END
// ELSE
// MySelf = vOldValue
// BREAK
// END
//END
//
//
Is it better? Well, it avoid a loop with different places where one has to break it
It avoids a double call to TableSearch, but replaces it with a recursive call
Other really good suggestions:
function to avoid a double in a two dimensional array
You can use a loop to iterate over the elements in the two-dimensional array and check if a given value already exists. Here’s an example implementation in C#:
csharpCopy codepublic bool CheckIfValueExistsInArray(int[,] array, int value)
{
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
if (array[i, j] == value)
{
return true;
}
}
}
return false;
}
In this example, the CheckIfValueExistsInArray 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 true. If the loop completes without finding a match, the method returns false.
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.
And:
How can I avoid the double loop in the suggested code?
If you want to avoid using nested loops, you can use the Array.IndexOf method to search for the value in a flattened version of the two-dimensional array. Here’s an example implementation:
csharpCopy codepublic bool CheckIfValueExistsInArray(int[,] array, int value)
{
int[] flattenedArray = new int[array.GetLength(0) * array.GetLength(1)];
Buffer.BlockCopy(array, 0, flattenedArray, 0, flattenedArray.Length * sizeof(int));
return Array.IndexOf(flattenedArray, value) != -1;
}
In this example, the CheckIfValueExistsInArray 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 Buffer.BlockCopy to copy the elements from the two-dimensional array to the new array. It then uses the Array.IndexOf method to search for the value in the flattened array. If the value is found, the method returns true. If the value is not found, the method returns false.
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.