为什么我们在VB.NET中使用IIF? [英] Why we use IIF in VB.NET?

查看:78
本文介绍了为什么我们在VB.NET中使用IIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If和IIF之间的区别?在这种情况下,使用IIF条件并使用条件?为什么我们在VB.NET中使用IIF?

解决方案

这已在较新版本中删除。我建议使用If-Else或case构造。要了解iif的作用,请查看此处 [< a href =http://msdn.microsoft.com/en-us/library/27ydhh0d%28v=vs.90%29.aspxtarget =_ blanktitle =New Window> ^ ] 。



基本上它会做这样的事情:



 iif([someCondition] ],[returnValueIfConditionIsTrue],[returnValueIfConditionIsFalse])


好。 IIF兼容VB6

更好地使用IF,它更强大,更好等等。



为什么我用它?它使代码缩短

示例

  Dim  As   Boolean  =  False  
Console.WriteLine(< span class =code-keyword>如果(this, ,< span class =code-string> No))


我会说:不要使用 Iif [ ^ ]功能,返回两个对象中的一个,具体取决于表达式的计算结果。为什么?使用lambda表达式有很多更好的编程习惯:

  Dim  boolVal  as   Boolean  =  False  

< span class =code-keyword> Dim result = 功能(x As 布尔作为 字符串
Dim sRetVal as String = yes
选择 案例 x
案例 true
sRetVal = yes
案例 False
sRetVal = no
结束 选择
返回 sRetVal
结束 功能

Console.WriteLine( 结果是:{0},结果(boolval))





以上示例非常简单。您可以说使用 iif 甚至如果函数更容易(如本问题的答案中所述) 。这是事实,但使用lambda表达式更灵活,因为提供了一种创建自定义函数的方法,可以在定义条件时验证条件并返回结果。



有关详细信息,请参阅:如何:创建Lambda表达式(Visual Basic) [ ^ ]



顺便说一句,C#更强大!在Vb.Net代码之上我们可以写成:

  string  result = boolVal ==  true   yes 没有; 
Console.WriteLine( 结果为:{0},结果);





干杯,

Maciej


Difference between If and IIF? In which case, IIF condition is used and If condition is used? Why we use IIF in VB.NET?

解决方案

This has been removed in newer versions. I would recommend making use of If-Else or case construct. To know what iif does, take a look here[^].

Basically it does something like this:

iif([someCondition],[returnValueIfConditionIsTrue],[returnValueIfConditionIsFalse])


Well. IIF is compatibility from VB6
better use IF instead, its more powerful, better, etc.

Why I use it? it makes code shorter
Example

Dim this As Boolean = False
Console.WriteLine(If(this, "Yes", "No"))


I'd say: do not use Iif[^] function, which returns one of two objects, depending on the evaluation of an expression. Why? There is much, much better programming practice to use lambda expressions:

Dim boolVal as Boolean = False

Dim result = Function(x As Boolean) As String
             Dim sRetVal as String = "yes"
                Select Case x
                    Case true
                        sRetVal = "yes"
                    Case False
                        sRetVal = "no"
                End Select
                Return sRetVal
                End Function

Console.WriteLine("The result is: {0}", result(boolval))



Above example is very simple. You may say that it's much easier to use iif or even if function (as is mentioned in answers to this question). It's true, but using lambda expressions is more flexible, because provides a way to create custom function that verifies condition and returns result as you define it.

For further information, please see: How to: Create a Lambda Expression (Visual Basic)[^]

By The Way, C# is more powerfull! Above Vb.Net code we can write as:

string result = boolVal==true ? "yes" : "no";
Console.WriteLine("The result is: {0}, result);



Cheers,
Maciej


这篇关于为什么我们在VB.NET中使用IIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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