Exforsys

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor

Ads


Home arrow Technical Training arrow VB.NET 2005

Windows Application Testing Page - 2

Page 2 of 2
Author : Exforsys Inc.     Published on: 30th Jul 2005    |   Last Updated on: 24th Dec 2007

Windows Application Testing

.

.

Ads

Conditional Compilation

Conditional compilation can be used to select particular sections of code to compile while excluding other sections. For example, debugging statements may have to be written, that compare the speed of different approaches to the same programming task, or an application may have to be localized for multiple languages. Conditional compilation statements are designed to run during compile time, not at run time.

A conditional compiler constant can be declared in code with the #Const directive, and blocks of code can be denoted to be conditionally compiled with the #If...Then...#Else directive. For example, to create French and German language versions of the same application from the same source code, platform-specific code segments will have to be embedded in #If...Then statements using the predefined constants FrenchVersion and GermanVersion. The following example demonstrates the point:

#If FrenchVersion Then
' < code specific to the French language version >.
#ElseIf GermanVersion Then
' < code specific to the German language version >.
#Else
' f the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch. These constants can be used only for conditional compilation; they cannot be used in executable code. This example uses the TARGET conditional compilation constant to determine whether to compile certain statements. The Visual Studio integrated development environment defines the CONFIG, DEBUG, and TRACE conditional compilation constants. The Visual Basic compiler defines the TARGET and VBC_VER conditional compilation constants. The TARGET and VBC_VER constants are not available in compiler versions before Visual Basic 2005

Parts
Expression
Required for #If and #ElseIf statements, optional elsewhere. Any expression, consisting exclusively of one or more conditional compiler constants, literals, and operators, that evaluates to True or False.

Statements
Required for #If statement block, optional elsewhere. Visual Basic program lines or compiler directives that are compiled if the associated expression evaluates to True.

#End If
Terminates the #If statement block.

Remarks
On the surface, the behavior of the #If...Then...#Else directives appears the same as that of the If...Then...Else statements. However, the #If...Then...#Else directives evaluate what is compiled by the compiler, whereas the If...Then...Else statements evaluate conditions at run time.

Conditional compilation is typically used to compile the same program for different platforms. It is also used to prevent debugging code from appearing in an executable file. Code excluded during conditional compilation is completely omitted from the final executable file, so it has no effect on size or performance.

Regardless of the outcome of any evaluation, all expressions are evaluated using Option Compare Binary. The Option Compare statement does not affect expressions in #If and #ElseIf statements.

It must be noted that no single-line form of the #If, #Else, #ElseIf, and #End If directives exists. No other code can appear on the same line as any of the directives.

Example
This example uses the #If...Then...#Else construct to determine whether to compile certain statements.

#Const CustomerNumber = 36
#If CustomerNumber = 35 Then
' Insert code to be compiled for customer # 35.
#ElseIf CustomerNumber = 36 Then
' Insert code to be compiled for customer # 36.
#Else
' Insert code to be compiled for all other customers.
#End If

#End If If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.
#End If If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.

#End If If the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.

#End IfIf the value of the FrenchVersion constant is set to True at compile time, the conditional code for the French version is compiled. If the value of the GermanVersion constant is set to True, the compiler uses the German version. If neither is set to True, the code in the last Else block runs. It must be noted here that Autocompletion will not function when editing code and using conditional compilation directives if the code is not part of the current branch.

Conditional Compilation Constants

Conditional compilation allows the user control at compile time what code to include in the program.

The following table lists the predefined constants available for conditional compilation.

Constant

Description

CONFIG

A string that corresponds to the current setting of the Active Solution Configuration box in the Configuration Manager.

DEBUG

A Boolean value that can be set in the Project Properties dialog box. By default, the Debug configuration for a project defines DEBUG. When DEBUG is defined, System.Diagnostics.Debug class methods generate output to the Output window. When it is not defined, System.Diagnostics.Debug class methods are not compiled and no Debug output is generated.

TARGET

A string representing the output type for the project or the setting of the command-line /target option. The possible values of TARGET are: "winexe" for a Windows application, "exe" for a console application, "library" for a class library, and "module" for a module. The /target option may be set in the Visual Studio integrated development environment. For more information, see /target.

TRACE

A Boolean value that can be set in the Project Properties dialog box. By default, all configurations for a project define TRACE. When TRACE is defined, System.Diagnostics.Trace class methods generate output to the Output window. When it is not defined, System.Diagnostics.Trace class methods are not compiled and no Trace output is generated.

VBC_VER

A number representing the Visual Basic version, in major.minor format. The version number for Visual Basic 2005 is 8.0.


These constants can be used only for conditional compilation; they cannot be used in executable code.

Example
This example uses the TARGET conditional compilation constant to determine whether to compile certain statements.

#If TARGET = "winexe" Then
' Insert code to be compiled for a Windows application.
#ElseIf TARGET = "exe" Then
' Insert code to be compiled for a console application.
#End If

Requirements
The Visual Studio integrated development environment defines the CONFIG, DEBUG, and TRACE conditional compilation constants.

The Visual Basic compiler defines the TARGET and VBC_VER conditional compilation constants. The TARGET and VBC_VER constants are not available in compiler versions before Visual Basic 2005

#If...Then...#Else Directives
Conditionally compiles selected blocks of Visual Basic code.

#If expression Then
statements
[ #ElseIf expression Then
[ statements ] ]
...
#ElseIf expression Then
[ statements ] ]
[ #Else
[ statements ] ]
#End If

Method

Output

Assert

The specified text; or, if none is specified, the Call Stack. The output is written only if the condition specified as an argument in the Assert statement is false.

Fail

The specified text; or, if none is specified, the Call Stack.

Write

The specified text.

WriteIf

The specified text, if the condition specified as an argument in the WriteIf statement is satisfied.

WriteLine

The specified text and a carriage return.

WriteLineIf

The specified text and a carriage return, if the condition specified as an argument in the WriteLineIf statement is satisfied.



 
This tutorial is part of a VB.NET 2005 tutorial series. Read it from the beginning and learn yourself.

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor
 

Comments