1

Is it in VB.NET possible to use variables without the need of use DIM?

now I have to use the variables like this:

dim a = 100
dim b = 50
dim c = a + b

I want to be able to use vars in this way:

a=100
b=50
c=a+b  'c contains 150

I think in VB6 and older VB this was possible, but I am not sure.

5
  • 5
    No, VB has grown up to be a real language today. Dim is required. Commented Jul 10, 2011 at 16:57
  • 1
    @Hans - not strictly true, but oh how I wish it was. Commented Jul 10, 2011 at 17:34
  • 7
    Sounds a lot like trying to make your car lighter by removing the brake pedal. Commented Jul 10, 2011 at 17:37
  • Can you provide a reason why on earth you would want to do this - and please don't say "so I don't have to type Dim every now and then!" Commented Jul 11, 2011 at 15:24
  • peronaly I use the dim, I asked only if it is possible because in one of my app I am using CodeDom where people can add their own vb code on the fly. For simple equations like c=a+123 I don't want the user to learn about dims or variable types. I want to keep the codedom part as simple as possible, just like in php, no dim. $a=123 and it works. Commented Jul 12, 2011 at 10:22

2 Answers 2

8

As far as what @Konrad said, he is correct. The answer, buried in all his caveat emptors, is the answer of "yes", you can absolutely do this in VB.NET by declaring Option Explicit Off. That said, when you do a=1, the variable a is NOT an Integer - it is an Object type. So, you can't then do c = a + b without compiler errors. You'll need to also declare Option Strict Off. And at that point, you throw away all the benefits of a compiler. Don't do it.

As an alternative, with Option Infer On, Dim behaves the same as C#'s var keyword and gives you a lot of advantages if you're trying to save on typing.

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

4 Comments

awesome. Option Explicit Off was what I needed - Will accept your answer so you get some points, Konrad's answer is also correct - thanks! :)
Um, correction: Option Explicit Off is what you think you need. You may have noticed that every single person here has warned you against doing this.
@qxxx As Jean-Francois pointed out, you only think you want this. One day after you spend hours chasing down a bug created by not wanting to type a few extra characters you will turn strict on, explicit on, and infer off.
He wants to make a simple scripting language such as that in the old Office products. I don't think what he's asking for is that bad, given the limited context he should be allowing.
8

You have a fundamental misunderstanding of how VB is supposed to work. The Dim statements are there to help you. Your wish to elide them is misplaced.

The compiler enforces variable declaration so that it can warn you when you have accidentally misspelt a variable name, thus inadvertently creating a new variable, and is required to enforce type safety. Without variable declaration, VB code becomes an unreadable, unmaintainable mess.

Incidentally, the same was true in VB6, and you should have used Option Explicit in VB6 to make the compiler force you to use them properly. This option still exists in VB.NET but switching it off has no advantage, and a whole lot of disadvantages so don’t do it – instead, learn to appreciate explicit variable declarations, and all the help that the compiler is giving you through them.

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.