Most programmers thought that earlier versions of VB were way too permissive
when it came to converting between types. This led to the phenomena of “evil
type conversion” where, for example, VB6 allowed you to multiply, say, a string of
numerals by an integer.
The option you have in VB .NET to make type conversion safe is called
Option Strict. You can turn this feature on by using:
Option Strict On
as the first line of code in any program you write. (You can also use the Build tab
of the Projects Properties dialog box.) Once you turn this option on (and you
should!), VB .NET requires you to explicitly make a conversion (sometimes called
a cast) whenever there is the possibility of loss of information (a lossy conversion,
to say it in the jargon). For example, when you convert a Single to an Integer, there
is the possibility of losing information. On the other hand, if there is no potential
of information loss (for instance, from an Integer to a Long or Decimal), VB .NET
automatically makes the conversion. The documentation for VB .NET refers to
these lossless conversions as widening conversions. Table 3–3 lists the permissible
widening conversions for basic types.
What is more, if you have the default of Option Strict on, then you cannot
have lines of code like:
Dim foo As Boolean
foo = 3
Table 3–3. Permissible Widening Conversions for Basic Types
TYPE WIDENS TO
Byte Byte, Short, Integer, Long, Decimal, Single, Double
Short Short, Integer, Long, Decimal, Single, Double
Integer Integer, Long, Decimal, Single, Double
Long Long, Single, Decimal, Double
No comments:
Post a Comment
This is Good Computer Information Blog.they will give
best information about computer.