如何以有效的方式或单行写入语句 [英] How Can I Write If Statement In Effective Way Or In Single Line

查看:61
本文介绍了如何以有效的方式或单行写入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If

                If sCon.sqlReader("cityID").ToString() <> "" Then
                    cboCity.SelectedValue = sCon.sqlReader("cityID")
                End If
                If sCon.sqlReader("StateID").ToString() <> "" Then
                    cboState.SelectedValue = sCon.sqlReader("stateID")
                End If
                If sCon.sqlReader("countryID").ToString() <> "" Then
                    cboCountry.SelectedValue = sCon.sqlReader("countryID")
                End If
            End If
        End If

推荐答案

请参阅VB.NET的语法BNFif: http://msdn.microsoft.com/en-us/library/752y8abs.aspx [ ^ ]。



单线语法可用,请参阅。< br $> b $ b





你知道吗?在你的问题中,这绝对是更糟糕的吗?使用cityID等,每种情况下重复两次。这是一个很大的禁忌。请尝试理解为什么你永远不应该做这样的事情。



第二个更糟糕的是重复几乎相同的if语句两次。我称它不是编程,与编程相反。请参阅:

http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ]。



-SA
Please see the syntax BNF for VB.NET "if": http://msdn.microsoft.com/en-us/library/752y8abs.aspx[^].

The single-line syntax is available, please see.



Do you know what is absolutely the worse in your question? The use of "cityID" and so on, repeated twice in each case. This is a big no-no. Please try to understand why you should never ever do such things.

Second worse is repeating nearly the same "if" statement two times. I call it not programming at all, something opposite to programming. Please see:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

—SA


不确定单行是什么意思......但我想我得到了你想要达到的目标。 br />


如果你想循环收集字段......



Not sure what single line means... but i think i got what you want to achieve.

If you want to loop through the collection of fields...

Dim i As Integer = 0 
Do 
    If sCon.sqlReader.Item(i).Value.ToString()<>String.Empty Then 
        Me.Controls("cbo" & Left(sCon.sqlReader.Item(i).ColumnName, Len(sCon.sqlReader.Item(i).ColumnName) - 2)) = sCon.sqlReader.Item(i)
        Exit Do
    End If
    i= i + 1
Loop Until sCon.sqlReader.Item(i).Value.ToString()=String.Empty


这篇关于如何以有效的方式或单行写入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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