What does it mean?
Putting the "Option Explicit" statement at the top of a code module (which includes forms/modules/classes/...) forces you to declare all variables that you have used in that module, using Dim or similar.
If you try to run your code when a variable hasn't been declared, it will be highlighted, and you will get a clear error: "Variable not defined"
Why should I use it?
You are probably thinking "errors are bad, I don't want that!", but this is actually a very good error - as it tells you about problems that are hard to spot otherwise.
Have a look at this code, can you see why it gives the wrong answers?
vb Code:
Dim MyVariable As Integer MyVariable = 10 MsgBox MyVariable 'should show 10 MyVariable = MyVariable + 1 MsgBox MyVariable 'should show 11 MyVariable = MyVaraible - 2 MsgBox MyVariable 'should show 9 Instead of showing us "10", "11", "9", the messages actually show us "10", "11", "-2"!
The reason for this is that I mis-spelt the variable name (MyVariable = MyVaraible - 2), so VB being 'kind' creates a new variable (which has a default value of 0), and uses it in the calculation.
In the code above it is fairly easy to spot the mistake, but the more code you have the harder it gets to find mistakes like this - generally all you know is that the code is not working properly, but you can't tell why.
Instead of spending lots of time trying to work it out, simply having Option Explicit at the top of the code file will tell you what (and where) the problem is, so all you need to do is correct the variable name (or declare the variable, if it was meant to be a different one!).
Can I have it added to my code automatically?
Yes you can, but only to new files that you create - you will need to add it yourself to existing files.
To have it added to all new files you create, simply select "Tools" -> "Options", and tick the "Require Variable Declaration" box.
ASP.NET is a development framework for building web pages and web sites with HTML, CSS, JavaScript and server scripting. ASP.NET supports three different development models: Web Pages, MVC (Model View Controller), and Web Forms.
Subscribe to:
Post Comments (Atom)
Using Authorization with Swagger in ASP.NET Core
Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models { ...
-
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.D...
-
<ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}"> <Borde...
-
<?xml version="1.0" encoding="utf-8"?> <Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting...
No comments:
Post a Comment