Conditional Compilation

1.13 Conditional Compilation

#Const Directive

 


 
#Const constantname = expression

 

constantname required; String literal

Name of the constant

expression required; literal

Any combination of literal values, other conditional compilation constants defined with the #Const directive, and arithmetic or logical operators except Is

Description

Defines a conditional compiler constant.

#If . . . Then . . . #Else Directive

 


 
#If expression Then
  statements
[#ElseIf furtherexpression Then
  [elseifstatements]]
[#Else
  [elsestatements]]
#End If

 

expression required

An expression made up of literals, operators, and conditional compiler constants that will evaluate to True or False

statements required

One or more lines of code or compiler directives, which is executed if expression evaluates to True

furtherexpression optional

An expression made up of literals, operators, and conditional compiler constants that will evaluate to True or False. furtherexpression is only evaluated if the preceding expression evaluates to False

elseifstatements optional

One or more lines of code or compiler directives, which is executed if furtherexpression evaluates to True

elsestatements optional

One or more lines of code or compiler directives, which are executed if expression or furtherexpression evaluates to False

Description

Defines a block or blocks of code that are only included in the compiled application when a particular condition is met, allowing you to create more than one version of the application using the same source code.

Conditionally including a block of code is a two-step process:

         Use the #Const directive to assign a value to a conditional compiler constant.

         Evaluate the conditional compiler constant using the #If...Then...#EndIf statement block.

Only code blocks whose expressions evaluate to True are included in the executable. You can use the #Else statement to execute code when the #If...Then expression evaluates to False. You can also use an #ElseIf statement to evaluate more expressions if previous expressions in the same block have evaluated to False.

CBool Function

 


 
CBool(expression)

 

expression required; String or Numeric

Any numeric expression or a string representation of a numeric value

Named Arguments

No

Return Value

expression converted to Boolean data type (True or False)

Description

Casts expression as a Boolean data type. When a numeric value is converted to Boolean, any nonzero value is converted to True, and zero is converted to False.

If the expression to be converted is a string, the string must be capable of being evaluated as a number, or it must be "True" or "False". Any other string generates an exception. For example, CBool("one") results in a type mismatch error, whereas CBool("1") is converted to True, and CBool("True") is converted to True.

CByte Function

 


CByte(expression)

 

expression required; String or Numeric

A string or numeric expression that evaluates to a number between 0 and 255

Named Arguments

No

Return Value

expression converted to Byte data type

Description

Converts expression to a Byte data type. If the expression to be converted is a string, the string must be capable of conversion to a numeric expression; this can be checked using the IsNumeric function. If expression evaluates to less than 0 or more than 255, an exception is generated. If the value of expression is not a whole number, CByte rounds the number prior to conversion.

CChar Function

 


CChar(expression)

 

expression required; String

Any string expression

Named Arguments

No

Return Value

A value of type Char

Description

Converts the first character in a string expression to a Char data type.

CDate Function

 


CDate(expression)

 

expression required; String or Numeric

Any valid representation of a date and time

Named Arguments

No

Return Value

expression converted into a Date data type

Description

Converts expression to a Date data type. The format of expression the order of day, month, and year is determined by the locale setting of the local computer. To be certain a date is recognized correctly by CDate, the month, day, and year elements of expression must be in the same sequence as the local computer's regional settings; otherwise, the CDate function has no idea, for example, that 4 was supposed to be the fourth day of the month, not the month of April. The earliest date that can be handled by the Date data type is 01/01/100. The latest date that can be handled by the Date data type is 12/31/9999.

CDbl Function

 


CDbl(expression)

 

expression required; Numeric or String

A number or a string representation of a number

Named Arguments

No

Return Value

expression cast as a Double data type

Description

Converts expression to a Double data type. If expression is not numeric, or if its value is outside the range of the Double data type, an exception is generated.

CDec Function

 


CDec(expression)

 

expression required; Numeric or String

A numeric value or a string representation of a number

Named Arguments

No

Return Value

expression cast as a Decimal type

Description

Converts expression to a Decimal data type. If expression is not numeric or is outside the range of the Decimal data type, an exception is generated. Use the IsNumeric function to test whether expression evaluates to a number.

CInt Function

 


CInt(expression)

 

expression required; Numeric or String

A numeric value or a string representation of a number

Named Arguments

No

Return Value

expression cast as an Integer

Description

Converts expression to an Integer data type. If expression has a fractional value, it is rounded. When the fractional part of expression is exactly .5, CInt always rounds it to the nearest even number. If expression is not numeric or is outside the range of the Integer data type, an exception is generated. Use IsNumeric to test whether expression evaluates to a number.

CLng Function

 


 
CLng(expression)

 

expression required; Numeric or String

A number or a string representation of a number

Named Arguments

No

Return Value

expression cast as a Long data type

Description

Converts expression to a long integer; any fractional element of expression is rounded. When the fractional part is exactly .5, CLng always rounds it to the nearest even number. If expression is not numeric or is outside the range of the Long data type, an exception is generated. Use IsNumeric to test whether expression evaluates to a number.

CObj Function

 


CObj(expression)

 

expression required; any

Expression to be converted to type Object

Named Arguments

No

Return Value

expression cast as an Object data type

Description

Converts any expression that can be interpreted as an object to Object. expression can be any data type, including a strongly typed object, as the following code fragment illustrates:

Dim oSomeClass As New CSomeClass
Dim oObj As Object
oObj = CObj(oSomeClass)

CSng Function

 


CSng(expression)

 

expression required; Numeric or String

The range of expression is -3.402823E38 to -1.401298E-45 for negative values, and 1.401298E-45 to 3.402823E38 for positive values.

Named Arguments

No

Return Value

expression cast as a Single data type

Description

Returns a single-precision number.

CStr Function

 


CStr(expression)

 

expression required; any

Any numeric, date, string, or Boolean expression

Named Arguments

No

Return Value

expression converted to a string

Description

Returns a string representation of expression.

CType Function

 


CType(expression, typename)

 

expression required; any

The data item to be converted

typename required; Keyword

The data type, object type, structure, or interface to which expression is to be converted

Named Arguments

No

Return Value

expression cast as a typename interface, object, structure, or data type

Description

Converts an expression to the specified data type if possible; expression can be any data, object, structure, or interface type. typename can be any data type (such as Boolean, Byte, Decimal, Long, Short, String, etc.), structure type, object type, or interface that can be used with the As clause in a Dim statement. If the function fails, or if the converted value of expression is outside the range allowed by typename, an InvalidCastException exception occurs.

DateValue Function

Microsoft.VisualBasic.DateAndTime


DateValue(stringdate)

 

stringdate required; String

A string containing any of the date formats recognized by IsDate

Return Value

A Date that represents the date specified by the stringdate argument

Description

Converts the string representation of a date to a Date data type while dropping any time component. The date value is formatted according to the short date setting defined by the Regional Settings applet in the Control Panel. DateValue can successfully recognize stringdate in any of the date formats recognized by IsDate, but the order of the day, month, and year within stringdate must be the same as that defined by the computer's regional settings. If you don't specify a year in your date expression, DateValue uses the current year from the computer's system date. DateValue does not return time values in a date/time string: they simply dropped. However, if stringdate includes an invalid time value, an exception results.

DirectCast Function

 


DirectCast(expression, typename)

 

expression required; any

The data item to be converted

typename required; Keyword

The data type, object type, structure, or interface to which expression is to be converted

Named Arguments

No

Return Value

expression cast as a typename interface or object

Description

Converts an expression to its runtime data type, if possible; otherwise, returns an error. expression must be a reference type, typically a variable of type Object. typename can be any data type (such as Boolean, Byte, Decimal, Long, Short, String, etc.), structure type, object type, or interface. If the function fails, an InvalidCastException exception occurs.

Option Strict Statement

 


 
Option Strict [On | Off]

 

Description

OptionStrict prevents VB from making any implicit narrowing data type conversions, since they may involve data loss. It also causes errors to be generated for late binding, as well as for any undeclared variables. Option Strict On implies Option Explicit On. For example:

Dim lNum As Long = 2455622
Dim iNum As Integer = lNum

converts a Long (whose value can range from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) to an Integer (whose value can range from 2,147,483,648 to 2,147,483,647). In this case, even though no data loss would result from the narrowing, OptionStrictOn would still not allow the conversion and would instead generate a compiler error.

If the OptionStrict statement is not present in a module, OptionStrict is Off. The default is OptionStrictOn. In other words, the statement:

Option Strict On

is equivalent to the statement:

Option Strict

The OptionStrict statement must appear in the declarations section of a module before any code.

The VB compiler considers the following to be narrowing conversions:

         Short, Integer, Long, Decimal, Single, DoubleByte

         Integer, Long, Decimal, Single, Double Short

         Long, Decimal, Single, Double Integer

         Decimal, Single, DoubleLong

         Single, DoubleDecimal

         DoubleSingle

         Integer type or wider Any enumerated type

         StringChar

         ObjectAny type

         Any base typeAny derived type

         An interfaceAny type implementing the interface

         Any typeNothing

         Conversions between Boolean and any numeric type

         Any numeric typeany enumerated type

         Conversions between a Char array and a String

         Conversions between String and any numeric, Boolean, or Date type

Str Function

Microsoft.VisualBasic.Conversion


Str(number)

 

number required; Numeric

Any valid numeric expression or expression capable of conversion to a number

Return Value

A String representation of number

Description

Converts number from a numeric to a string. If the return value is positive, the Str function always includes a leading space in the returned string for the sign of number.

TimeValue Function

Microsoft.VisualBasic.DateAndTime


TimeValue(stringtime)

 

stringtime required; String

Any valid string representation of a time

Return Value

A Date containing the time specified by the string time argument, with the date set to January 1 of the year 1 CE.

Description

Converts the string representation of a time to a Date type containing the time. stringtime can be in any time format recognized by the IsDate function. Both 12- and 24-hour clock formats are valid. The Date value returned by time is formatted based on the system's regional settings. If stringtime is invalid or is Nothing, an exception is raised.

Val Function

Microsoft.VisualBasic.Conversion


Val(expression)

 

expression required; String or Char

Any string representation of a number

Return Value

A Double able to hold the number contained in expression

Description

Converts a string representation of a number into a Double. The Val function starts reading the string with the leftmost character and stops at the first character that it does not recognize as being part of a valid number. For example, the statement:

iNumber = Val("1A1")

returns 1.

&O and &H (the octal and hexadecimal prefixes) are recognized by the Val function and the period (.) is recognized as a decimal delimitor. Currency symbols, such as $ and , and delimiters, such as commas, are not recognized. Prior to processing expression, Val removes spaces, tabs, and line-feed characters.

ValDec Function

Microsoft.VisualBasic.Conversion


ValDec(expression)

 

expression required; String or Char

Any string representation of a number

Return Value

A Decimal able to hold the number contained in expression

Description

Converts a string representation of a number into a Decimal. The ValDec function starts reading the string with the leftmost character and stops at the first character that it does not recognize as being part of a valid number. For example, the statement:

iNumber = ValDec("1A1")

returns 1.

&O and &H (the octal and hexadecimal prefixes) are recognized by the ValDec function. Currency symbols, such as $ and , and delimiters, such as commas, are not recognized as numbers by the ValDec function. The ValDec function only recognizes the period (.) as a decimal delimiter. Prior to processing expression, ValDec removes spaces, tabs, and line-feed characters.

 



VB. NET Language Pocket Reference
VB.NET Language Pocket Reference
ISBN: 0596004281
EAN: 2147483647
Year: 2002
Pages: 31

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net