4

there is the option to do this in 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?

3
  • 3
    Do you really think such a coding style would be clearer than a separate statement per variable? Commented Feb 3, 2013 at 20:51
  • I am applying that style to a function already tested and well commented so I think in that case is good to do so Commented Feb 3, 2013 at 20:53
  • 1
    Absolutely not - just because a function is already tested and commented doesn't mean you should feel free to make the implementation unreadable. Declaring lots of variables - of different types, no less! - in the same statement and assigning them values - sounds like a way of making it take much longer to read and understand. Commented Feb 3, 2013 at 20:56

2 Answers 2

5

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

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):

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, Double and Integer. You could also use F for single. See: Constant and Literal Data Types (Visual Basic)

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.