Results 1 to 7 of 7

Thread: [2005] Try/Catch Code correctness, advice(seeking).

  1. #1

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    [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.

  2. #2
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    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..."

  3. #3
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    675

    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..."

  4. #4
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    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

  5. #5
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  6. #6

    Thread Starter
    Lively Member smilbuta's Avatar
    Join Date
    Apr 2005
    Location
    Orlando
    Posts
    104

    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..

  7. #7
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] Try/Catch Code correctness, advice(seeking).

    no problem
    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
  •  



Click Here to Expand Forum to Full Width