[2005] Try/Catch Code correctness, advice(seeking).
Hello community.
I am handling a nothing value for a set of variables and i want to know what is the best way to do this code wise. This is not an error question but more of how i can improve consiceness of the code.
App info: the combo boxes (usename and status) are databound to tables in sql database. Im dealing with an error if a user trys to type in the feild and submit. since the user enterd data has a selected value of Nothing according to VS , i can throw an error informing the user of his stup.. AHEM!.. issue.
Currently my code looks like the following.
Code:
Try
If cboUserName.SelectedValue Is Nothing Then
MessageBox.Show("woops, invalid name")
Else
userIdValG = CStr(cboUserName.SelectedValue)
End If
If cboStatus.SelectedValue Is Nothing Then
MessageBox.Show("woops, invalid status.")
Else
statusIdValG = CStr(cboStatus.SelectedValue)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
The core of my question deals with my IF statments and is ther a more efficient way to make that code more consice. In the actual app i have 7 combo boxes and the over all code would be slightly long and seemingly bloated... any advice welcome.. Thanx!!!
Re: [2005] Try/Catch Code correctness, advice(seeking).
Set LimitToList = True for all your comboboxes in the designer, then the user won't be able to type things in that aren't in the list.
Re: [2005] Try/Catch Code correctness, advice(seeking).
Sorry, I use some third party controls with enhanced properties. However, stopping the user from typing something that does not exist in the list would be the ideal solution.
Re: [2005] Try/Catch Code correctness, advice(seeking).
There really isn't much that I would change. Since you say you go through this type of thing 7 times, I would probably make a function that handles everything for you.
Code:
userIdValG = CheckForNothing(cboUserName, "woops, invalid name")
statusIdValG = CheckForNothing(cboStatus, "woops, invalid status.")
Private Function CheckForNothing(ByVal inComboBox as ComboBox, ByVal inErrorMessage As String) As String
Dim RetVal as String = String.Empty
If inComboBox.SelectedValue Is Nothing Then
MessageBox.Show(inErrorMessage)
Else
RetVal = cboStatus.SelectedValue.ToString()
End If
Return RetVal
End Function
Why not change the DropDownStyle property of the ComboBox to DropDownList. That way the user cannot type into the field. They have to select a value from the list.
Re: [2005] Try/Catch Code correctness, advice(seeking).
You don't need to use a try catch. Use a validating event, inside that event you can stipulate what can be put in and what can not and before focus from that control has completely changed the user will get a message box or some other form of notification that you deem telling them they did something wrong.
Re: [2005] Try/Catch Code correctness, advice(seeking).
Ohh i like the function method.. seems i can use its fuction elsewhere in my code!!
THanx all..
Re: [2005] Try/Catch Code correctness, advice(seeking).