goto语句是否有其他替代方法 [英] Is there any alternative method to goto statement
问题描述
我有6个文本框.
点击保存按钮后,应检查以下内容
如果在文本框4中输入的值以400开头,则保存.
否则,如果文本框4中的值不是以400开头,则与数据库一起检查该值是否存在于数据库中.
如果存在,则保存条目
否则检查文本框5.如果文本框5中的值为29或31,则保存
否则清除字段或不保存..
请帮我.
我是vb.net的新手...
请
我非常需要它....
我在这里使用了goto语句.还有其他方法可以..你能帮我吗?还是可以继续...找到下面的代码
I have 6 text boxes.
on clicking save button it should check the following
if the value entered in textbox 4 is beginning with 400 then save.
else if value in textbox 4 is not starting with 400 then check with database whether the value exists in database.
if exists save entry
else check for textbox 5. if value in textbox 5 is 29 or 31 then save
else clear field or dont save..
please help me.
i am new to vb.net...
please
i need it very immediately....
I have used goto statement here. Is there any other way to do this.. can u please help me ... or is this fine to proceed... find the below code
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
If (IsNumeric(txtAccNo.Text) = False Or txtAccNo.Text.Trim.Length < 12) Then
ShowMessage("Enter Account Number")
''some validation done here for first three textboxes..
If Mid(ctrl.txtBank_Code.Text, 1, 3) = "400" Then
GoTo repeat
End If
If Mid(ctrl.txtBank_Code.Text, 1, 3) <> "400" Then
Dim Conn1 As New SqlConnection(CONNSTR)
Dim cmd1 As New SqlCommand("", Conn1)
cmd1.CommandText = "Select MICR_CODE from New_Speed1 where MICR_CODE=''" & _
ctrl.txtBank_Code.Text & "''"
Try
Conn1.Open()
Dim drReader As SqlDataReader = cmd1.ExecuteReader
Dim isthr As Boolean = False
If drReader.HasRows Then
isthr = True
''MsgBox("TEST")
Conn1.Close()
cmd1.Dispose()
Conn1.Dispose()
drReader.Read()
ctrl.txtBank_Code.Text = drReader.Item("MICR_CODE")
drReader.Close()
Conn1.Close()
cmd1.Dispose()
Conn1.Dispose()
ElseIf isthr <> True And ((ctrl.txtTran_Code.Text = "29") Or (ctrl.txtTran_Code.Text = "31")) Then
GoTo repeat
ElseIf isthr <> True And (ctrl.txtTran_Code.Text <> "29" Or ctrl.txtTran_Code.Text <> "31") Then
MsgBox("OUT STATION !!!")
Exit Sub
End If
Catch ex As Exception
MsgBox("Could not Find Record", MsgBoxStyle.Critical)
End Try
End If
goto语句在这里.单击保存按钮后,它应如上所述进行验证并保存在数据库中
重复:
the goto statement comes here. once i click save button it should validate as mentioned above and save in database
repeat:
Dim cmd As New SqlCommand("Insert_Scanned_Cheques_New01", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("Account_No", Data.SqlDbType.VarChar, 12).Value = _
Me.txtAccNo.Text
推荐答案
只是给你一个主意:
我好久没见过一个goto
了.直到1980x中期,我才看到goto
的痕迹,主要是在FORTRAN中,几乎没有.即使在FORTRAN中,它也被认为已经过时了.这只是因为人们曾经在较早版本的编译器中工作.
我很惊讶有人记住此运算符并使用它.修复您的代码相对困难,因为普通程序员的大脑不习惯在goto
中看到任何需求.甚至不考虑使用.
我会尝试扔掉代码并正常编写.
顺便说一句,您滥用了try-catch.显示消息框后,将阻止异常传播.应该在主UI线程周期中捕获该异常. Forms和WPF的Application类都具有在周期的最开始捕获异常的成员.
您还会混淆数据操作和UI,这是非常糟糕的,并且不被支持.有不同的架构模式可帮助分离关注点:MVC,MVP,MVA和MVVM;您可以在Wikipedia中找到它们,请查看我过去的回答中的列表:
如何通过以下方式控制用户界面表单的Controlls函数(方法) [ ^ ].
—SA
Just to give you an idea:
I haven''t seen a singlegoto
for many ears. I saw the bare traces ofgoto
until middle of 1980x, mostly in FORTRAN and almost never later. Even in FORTRAN it was considered obsolete; it''s just because people used to work in earlier versions of the compiler.
I''m very surprised someone remembers this operator and use it. It''s relatively hard to fix your code because the normal programmer''s brain is not used to see any need ingoto
; the use if it is not even considered.
I would try to throw out the code and write it normally.
By the way, you misuse try-catch. After showing message box propagation of the exception is blocked. The exception should be caught in main UI thread cycle. The class Application of both Forms and WPF have the member to catch the exception on the very top of the cycle.
You also mix-up data manipulations and UI, this is very bad and not supportable. There are different architectural patterns helping to separate concerns: MVC, MVP, MVA and MVVM; you can find them in Wikipedia, please see the list in my past answer:
how to control Controlls of a user interface form through Functions (methods)[^].
—SA
这篇关于goto语句是否有其他替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!