c# - 此按钮操作是否正确编程
问题描述
看看我的第一个应用程序,我在正确的轨道上吗?我有一个简单的表单,用户输入要在数组中查找的值。如果找到带有索引的返回值。我使用 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);
,而不是仅为任务声明一个变量。我们倾向于只在需要创建任务,做更多工作,然后等待任务时才这样做
推荐阅读
- android - React Native :app:transformDexArchiveWithExternalLibsDexMergerForDebug Android 构建中的错误
- python - 将参数传递给端点以在事件挂钩中执行的函数内使用
- angular - angularfirebase 和 typescript - 设置数据库返回值的类型
- oracle - 安装多个版本的 Oracle Apex
- ios - 选择 segue 后,Storyboard 在 Xcode 11 中部分崩溃
- java - IntelliJ IDEA:生成带有特定类的 JAR?
- mysql - 将子子项中的外键添加到“超级父项” - 有没有像视图这样的更好方法?
- azure-devops - 发布测试结果任务中的“未找到匹配 **/*.trx 的测试结果文件”
- c - '=' 标记之前的预期表达式
- reactjs - 无法获取未定义或空引用的属性“包含”