Results 1 to 5 of 5

Thread: VbNet2008 Using GoTo

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    207

    VbNet2008 Using GoTo

    Hi Good Guys,
    I have been asked to update VB6 coding to VBNET2008. I am curious about the command GOTO even thought I have used it I did not test the coding yet.
    Before I test it, Please let me know it is ok to use GOTO command

    Here are the VBNET2008 coding:
    Code:
      'validate pagEdit data 
        Private Function FCheckData() As Boolean
            Dim bolFlag As Boolean = True
            Dim strmsg As String
    
            Try
                If Me.txtOrderID.TextLength = 0 Then
                    bolFlag = False
                    Me.txtOrderID.Focus()
                    GoTo FExit
                End If
                If lblOrderDate.Text = "" Then
                    bolFlag = False
                    strmsg = "Order Date is missing"
                    Me.btnOrdDte.Focus()
                    GoTo FExit
                End If
                If lblReqDate.Text = "" Then
                    bolFlag = False
                    strmsg = "Required Date is missing"
                    Me.btnReqDte.Focus()
                    GoTo FExit
                End If
                
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                bolEdit = bolFlag
            End Try
    
    FExit:
            If bolFlag = False Then
                MessageBox.Show(strmsg, "Update Cancelled ", MessageBoxButtons.OK)
            End If
    
            Return bolFlag
    
        End Function

    Have a Good Day.

    Cheers,
    Lennie

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: VbNet2008 Using GoTo

    It all comes down to personal preference, as no one can tell you how to write your code (except for the ones paying your salary!), but most people would agree with me that using GoTos in VB.NET is very bad practise.
    Whenever you feel the need for a GoTo, it's generally a good idea to think about your code design, or you'll end up with so much spaghetti code you can almost taste it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: VbNet2008 Using GoTo

    Code:
        Private Function FCheckData() As Boolean
            Dim bolFlag As Boolean = True
            Dim strmsg As String
    
            If Me.txtOrderID.TextLength = 0 Then
                bolFlag = False
                Me.txtOrderID.Select()
            ElseIf lblOrderDate.Text = "" Then
                bolFlag = False
                strmsg = "Order Date is missing"
                Me.btnOrdDte.Select()
            ElseIf lblReqDate.Text = "" Then
                bolFlag = False
                strmsg = "Required Date is missing"
                Me.btnReqDte.Select()
            End If
    
            bolEdit = bolFlag
    
            If bolFlag = False Then
                MessageBox.Show(strmsg, "Update Cancelled ", MessageBoxButtons.OK)
            End If
    
            Return bolFlag
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: VbNet2008 Using GoTo

    Or check all errors at once, tell the user about all of them, and then .Select the appropriate control

    Code:
        <FlagsAttribute()> _
        Enum Errors As Integer
            None = 0
            OrderID = 1 << 0
            OrderDate = 1 << 1
            ReqDate = 1 << 2
        End Enum
    
        Private Function FCheckData() As Boolean
            Dim bolFlag As Errors = Errors.None
            Dim strmsg As String
    
            If Me.txtOrderID.TextLength = 0 Then bolFlag = bolFlag Or Errors.OrderID
            If lblOrderDate.Text = "" Then bolFlag = bolFlag Or Errors.OrderDate
            If lblReqDate.Text = "" Then bolFlag = bolFlag Or Errors.ReqDate
            
            If Not (bolFlag = Errors.None) Then
                MessageBox.Show("Errors = " & bolFlag.ToString, "Update Cancelled ", MessageBoxButtons.OK)
                'select first control with error
                If (bolFlag And Errors.OrderID) = Errors.OrderID Then
                    'select control
                ElseIf (bolFlag And Errors.OrderDate) = Errors.OrderDate Then
                    'select control
                ElseIf (bolFlag And Errors.ReqDate) = Errors.ReqDate Then
                    'select control
                End If
                Return False 'errors
            Else
                Return True 'no errors
            End If
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2010
    Location
    New Zealand
    Posts
    207

    Re: VbNet2008 Using GoTo

    Hi Atheist and dbasNett

    Thanks to both of you for sharing information with me and for guiding me. Both of you are just awesome in sharing information.

    I will try out the sample coding and have a discussion with the System Analyst and explain to him the VBNET coding standard.



    Have a Good Day.

    Cheers,
    Lennie

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