简体   繁体   English

VB.Net中的三元运算

[英]Ternary operation in VB.Net

I am using VB.Net with Visual Studio 2019我在 Visual Studio 2019 中使用 VB.Net

Public Shared intDbEnvironment As Integer = CInt(Command().Split(" ")(0))

Sometimes if no parameter is passed, the app crashes.有时如果没有传递参数,应用程序就会崩溃。 Is there a ternary operator or something that I could use that sets the value to anything if its Nothing or "" or not a number是否有三元运算符或我可以使用的东西,如果它的 Nothing 或 "" 或不是数字,则可以将值设置为任何值

I've tried我试过了

Public Shared intDbEnvironment As Integer = Int32.Parse((Command().Split(" ")(0)))

TryParse Code尝试解析代码

 Dim success As Boolean = Int32.TryParse((Command().Split(" ")(0)), intDbEnvironment)
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim i As Integer
    Integer.TryParse(TextBox1.Text, i)
    Debug.Print(i.ToString)
End Sub

If I enter a non integer in the text box - Guess what!如果我在文本框中输入一个非整数 - 猜猜看! I get 0. Because the Function will not attempt to add a value to i if it is not an integer.我得到 0。因为如果它不是整数,函数将不会尝试向 i 添加值。 The default value of and Integer is 0 so that is the value from the declare.和 Integer 的默认值为 0,因此这是声明中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM