[英]Issue with ternary operator syntax in VB.NET
I'm trying to use a ternary operator in vb.net but to determine what to append to a string but I am having issues with syntax... 我正在尝试在vb.net中使用三元运算符,但是要确定要追加到字符串的内容,但是语法有问题...
Why does line three give an error on == generates expression expected error 为什么第三行在==上给出错误, 生成表达式预期错误
Dim sb As New StringBuilder
Dim bln As Boolean
bln == true ? sb.append("True") : sb.Append("False")
' this also doesn't work
bln ? sb.append("True") : sb.Append("False")
The ternary operator in VB.NET is used like this: VB.NET中的三元运算符的用法如下:
If(<Test Statement>, <Result if True>, <Result if False>)
So you could do this: 因此,您可以这样做:
sb.Append(If(bln, "True", "False"))
But this would be a better way to accomplish what you need: 但这将是完成您需要的更好的方法:
sb.Append(CStr(bln))
On C based languages it use to be like: 在基于C的语言上,它像是:
String name = (person == null) ? "" : person.Name;
But in VB.Net it is like: 但是在VB.Net中,它就像:
Dim name As String = If(person Is Nothing, "", person.Name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.