Download SNAP Getting Started Guide
Transcript
■ Omitting Variable Declarations When you’re just experimenting with small programs, declaring variables is sometimes not very helpful, and the extra typing and text just interfere with your thought process. If you put Option Explicit Off at the beginning of your program, then this will prevent the compiler from complaining about missing declarations, and this might make your life easier (for a while, anyway). On the other hand, as we saw in chapter 3, declaring variables helps the compiler find mistakes for you, so it’s valuable. When you write Option Explicit Off, the compiler doesn’t know the types of undeclared variables, so it assumes that they are all of type System.Object. As we will see later, all objects in Visual Basic are derived (either directly or indirectly) from System.Object, so a variable of this type can hold any value whatsoever, and any assignment statement will work, no matter how peculiar: circ = 3.75 circ = "hello" circ = Circle(2, 3, 1) r = circ.Radius ' ' ' ' circ is of type System.Object so this strange assignment works and so does this works, but we get no help from Intellisense When a variable is of type System.Object, you don’t get much help from Visual Studio Intellisense. When you type the dot in the fourth line of code above, you might be hoping to see a helpful list of the properties of an NX.Arc object, but you won’t, because the compiler thinks that circ is a System.Object, not an NX.Arc. If you get tired of declaring variables, but you still want the compiler to find your mistakes, and give you helpful Intellisense hints, then a good compromise is Option Infer On. With this option, the compiler tries to guess the type of a variable, based on its initialization or first usage. The code looks like this: Dim Dim Dim Dim Dim x = 3.75 y = Math.SinD(x) greeting = "hello" circ = Circle(2, 3, 1) r = circ.Radius ' ' ' ' ' Compiler guesses that x is of type Double Compiler guesses that y is of type Double Compiler guesses that greeting is of type String Compiler guesses that circ is of type NX.Arc Intellisense helps us, now The word Dim before a variable is what prompts the compiler to start guessing. You are still declaring the variables x, y, greeting, and circ, but you don’t have to tell the compiler their types, because it can guess from the context. This can cut down on a lot of repetition, and make your code much easier to read. In the following, the second three lines of code are much clearer than the first three, and just as safe: Dim p1 As NX.Point = Point(3,4) Dim q1 As NX.Point = Point(7,9) Dim a1 As NX.Line = Line(p1, q1) Dim p2 = Point(2,5) Dim q2 = Point(6,8) Dim a2 = Line(p2, q2) In the examples later in this document, and in the SNAP Reference Guide, we will sometimes use Option Infer On to make the code shorter and easier to read. You have to be a little careful, sometimes, because the guessing isn’t foolproof. Consider the following code: Dim r = 3 Dim circ = Circle(0, 0, 5.75) r = circ.Radius ' Compiler assumes that r is an Integer ' Create circle with radius 5.75 ' Error or unwanted rounding The compiler will infer that r is an Integer. So, in the third line of code, we’re trying to assign a Double value to an Integer variable, and we’ll either get an error message, or the value of circ.Radius will be rounded to 6 (instead of 5.75) when it’s stored in the variable r. To avoid this sort of problem, you can write Dim r = 3.0 in the first line, which will tell the compiler that r is supposed to be a Double. Getting Started with SNAP Chapter 4: The Visual Basic Language Page 32