|
-
Apr 18th, 2007, 10:38 AM
#1
Thread Starter
Lively Member
[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!!!
Last edited by smilbuta; Apr 18th, 2007 at 10:42 AM.
-
Apr 18th, 2007, 10:50 AM
#2
Fanatic Member
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.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Apr 18th, 2007, 10:52 AM
#3
Fanatic Member
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.
VB.Net 2008
.Net Framework 2.0
"Must you breathe? 'Cause I need heaven..."
-
Apr 18th, 2007, 11:05 AM
#4
Fanatic Member
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.
My.Settings.Signature = String.Empty
-
Apr 18th, 2007, 11:06 AM
#5
Addicted Member
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.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 18th, 2007, 11:17 AM
#6
Thread Starter
Lively Member
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..
-
Apr 18th, 2007, 11:27 AM
#7
Addicted Member
Re: [2005] Try/Catch Code correctness, advice(seeking).
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|