初始化并赋值给多个变量 vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14677155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:10:18  来源:igfitidea点击:

initialize and assign value to multiple variables vb.net

vb.netvisual-studio-2010variables

提问by cMinor

there is the option to do this in vb.net

可以选择在 vb.net 中执行此操作

Dim a, b, c As Single, x, y As Double, i As Integer

also you can do

你也可以做

Dim myString = String.empty

Is there a way to initialize and assign multiple variables like the first example but using the assignment as the second one?

有没有办法像第一个例子一样初始化和分配多个变量,但使用分配作为第二个例子?

回答by Olivier Jacot-Descombes

I tested it in VS 2008, but I don't think that the Syntax of the Dim statement changed since. You will have to specify the type for each variable, if you initialize them

我在 VS 2008 中对其进行了测试,但我认为 Dim 语句的语法从那以后没有改变。如果初始化它们,则必须为每个变量指定类型

Dim a As Single = 1, b As Single = 2, x As Double = 5.5, y As Double = 7.5

Or you can let VB infer the type and use type characters (here !for Single):

或者您可以让 VB 推断类型并使用类型字符(此处!Single):

Dim a = 1!, b = 2!, c = 3!, x = 5.5, y = 7.5, i = 100

By hovering with the cursor over the variables in VS you will see that the types are inferred correctly as Single, Doubleand Integer. You could also use Ffor single. See: Constant and Literal Data Types (Visual Basic)

通过将光标悬停在 VS 中的变量上,您将看到类型被正确推断为Single,DoubleInteger。您也可以F用于单身。请参阅:常量和文字数据类型 (Visual Basic)

回答by user1744515

I just now checked this code for multiple controls declaration in one line. It compiled,and window appeared successfully.It was part of a a very big project.**Dim DllNumber As New TextBox,DllMethod As New TextBox,LoopBox As New TextBox** kvinvisibleguy

我刚刚在一行中检查了此代码的多个控件声明。编译成功,窗口出现。这是一个非常大的项目的一部分。** Dim DllNumber As New TextBox,DllMethod As New TextBox,LoopBox As New TextBox** kvinvisibleguy