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