[英]Is this button action programmed correctly
看看我的第一个应用程序,我在正确的轨道上吗? 我有一个简单的表单,用户输入要在数组中查找的值。 如果找到带有索引的返回值。 我使用 VS 2019/C#。
private async void ButtonSingleGetValues_Click(object sender, RoutedEventArgs e)
{
//Tasks:
//Get Value from TextBox on Form
//Validate TextBox (TextBoxSingleArray.Text) is a number
//Check if Number exists in Array
//Number Exists, Return Number to Form TextBox to show it exists
//then return Index of Number to Form TextBox in Array
//Here is the single Array
int[] singleDimension = new int[5] { 1, 5, 10, 15, 20 };
//Grab value from user input textbox for single array
bool intSingleTryParse = int.TryParse(TextBoxSingleArray.Text, out int ValueSingle);
if (intSingleTryParse) //bool is true, continue
{
int ArrayValue = Array.Find(singleDimension, element => element == ValueSingle);
if (ArrayValue > 0)
{
TextBoxSingleArrayValue.Text = ArrayValue.ToString();
int ArrayPosition = Array.IndexOf(singleDimension, ValueSingle);
TextBoxSinglePosition.Text = ArrayPosition.ToString();
}
else
{
MessageDialog MsgNotFound = new MessageDialog("Did not find number in array!");
await MsgNotFound.ShowAsync();
}
}
else
{
MessageDialog MsgNope = new MessageDialog("You not a number!");
await MsgNope.ShowAsync();
}
}
}
}
我的版本是:
private async void ButtonSingleGetValues_Click(object sender, RoutedEventArgs e)
{
//I wouldn't use the word "single" in an array of ints because single is a floating point number type
//use plurals for collections
//do not need to specify array size with initializer
int[] numbers = new int[] { 1, 5, 10, 15, 20 };
//name your result bool more simply, do not mention single
//name your text box more simply
//windows controls like textbox tend to be named with the textbox word at the end
bool success = int.TryParse(FindIntTextBox.Text, out int findInt);
//personally, I prefer "test bad input, then return if it's bad"
//instead of ending up with nested nested code blocks that run for screens at a time
//it's kinda anathema to the "never use return mid method" crowd though
if (!success)
{
await new MessageDialog("Not a valid integer in the ... box").ShowAsync();
return;
}
//local variables are namedLikeThis, private are typically _namedLikeThis and public are NamedLikeThis
//do not use Array.Find on simple ints etc: you know what you're finding
//array.Find would be used for looking up eg a person by name
//you already know the value to find and just want its index
int foundIndex = Array.IndexOf(numbers, findInt);
//If there is a way to structure a test so that there is
//either a resulting one line operation to perform or a ten line op, i
//tend to place the the shorter code in the if and the longer
//code in the else. This way the condition that applies to
//the else is likely still visible on screen whereas if the if is
//long the else can lose meaning without a scroll. I could
//also have used the "if bad return" here but I do subscribe
//to "avoid return mid method if possible" and we've already started our processing
if(foundIndex == -1)
await new MessageDialog("not a known number").ShowAsync();
else
{
//not sure why you'd bother unless the int parse tolerated eg spaces
FindIntTextBox.Text = findInt.ToString();
FoundIndexTextBox.Text = foundIndex.ToString();
}
}
}
我会让异步等待调用单行await x.YAsync(z);
而不是仅仅为任务声明一个变量。 我们倾向于只在需要创建任务,做更多工作,然后等待任务时才这样做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.