嵌套if then语句,正确的方式来处理 [英] nested if then statement, proper way to handle

查看:496
本文介绍了嵌套if then语句,正确的方式来处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私人小组btn_Click
昏暗intAnswer为整型
如果txtAddress<>中那
intAnswer = MSGBOX你要保存,vbYesNo
    如果intAnswer = vbNo然后
其他
    做的事情
结束如果
结束小组
 

注意第二个然后语句是空白,我希望它去的else语句。我的大脑是玉米粥和放大器;我告诉自己最初做了 SELECT CASE ,但我不能算出它的方式。最后我做下面的&放大器;我不知道这是正确的步骤/建议。

 暗淡intAnswer为整型
如果txtAddress<>中那
intAnswer = MSGBOX你要保存,vbYesNo
    如果intAnswer = vbNo然后转到启动
其他
    转到启动
结束如果
退出小组
开始:
做的事情
 

解决方案

有一个更短的方式:

 如果txtAddress<>中或MSGBOX(你要保存,vbYesNo)= vbYes然后
        做的事情
结束如果
 

Private Sub btn_Click
Dim intAnswer as Integer
If txtAddress<>"" Then
intAnswer=Msgbox "Did you want to save", vbYesNo
    If intAnswer=vbNo Then 
Else
    do things
End If
End Sub

Notice the second Then statement is blank, I want it to go to the else statement. My brain is mush & I told myself initially do a SELECT CASE but I can't figure it out that way. I ended up doing the below & am not sure if that's proper procedure/recommended.

Dim intAnswer as Integer
If txtAddress<>"" Then
intAnswer=Msgbox "Did you want to save", vbYesNo
    If intAnswer=vbNo Then GoTo Start
Else
    GoTo Start
End If
Exit Sub
Start:
do things

解决方案

There's a much shorter way:

If txtAddress<>"" Or Msgbox("Did you want to save", vbYesNo) = vbYes Then
        'Do things
End If

这篇关于嵌套if then语句,正确的方式来处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆