分享
 
 
 

Conditional Compilation

王朝asp·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

Conditional Compilation

Although debugging techniques can help you iron out problems in your code,

you may want to simply add chunks of code at compile time (if you're

currently in the debugging phase of your project) or remove the same chunks

when you release your code. Conditional compilation makes it possible to

add and remove chunks from the executable.

Conditional compilation relies on compiler directives in your code that

indicate to the compiler, as it's compiling your code, how it should use

the code. Compiler directives are #If statements that direct the compiler

as to which code it should leave in and which code it should leave out. For

example, you might wish to put debugging code in your application, but when

you create a final compile, you don't want the debugging code to be there.

Or maybe you wish to take out the code for certain features if you are

creating a demo version of your product.

To use conditional compilation, you will first need to declare a compiler

constant. You declare then using the #Const statement. Each constant is

given a unique name and assigned a value. Once these constants are

declared, you can use them within an #If… #End If block. As you'll see,

there are several ways to define conditional compilation constants.

Declaring a File-Level Compiler Constant

You can declare a compiler constant anywhere within a file in your program.

That compiler constant is then available anywhere within that file. It does

not matter whether you place the constant declaration at the top of the

file, within a defined class, or within a procedure, it will still be

available throughout the whole file. For example, the sample page

DebugForm.aspx defines the following constant at the top of its code-behind

file:

#Const DEMO = True

Using Conditional Compilation Constants

To create a compiler directive, use the #If… #End If construct. You might,

for example, want to include one block of code for demo versions of your

application and different code for retail versions, like this:

Private Sub ConditionalSample()

#If DEMO Then

lblMessage.Text = "Demo version"

#Else

lblMessage.Text = "Retail version"

#End If

End Sub

NOTE

You may be wondering why you'd use conditional compilation rather than

simple conditional statements in your code. One good reason is that you may

have large blocks of code that are different for two versions of your

application. There's no reason to load both sets of code when they're

mutually exclusive. Instead, you can make the choice as to which block you

want to include at compile time (not at runtime) using conditional

compilation.

Unlike a normal If statement, the #If statement actually removes the unused

code as it compiles. This leads to a reduced application size in memory and

prevents unnecessary or unwanted code being deployed as part of the

application. For the preceding example, the compiler will actually only see

the following code (assuming that the DEMO constant is set to True):

Private Sub ConditionalSample()

lblMessage.Text = "Demo version"

End Sub

TIP

Visual Studio .NET provides two built-in compile-time constants: DEBUG and

TRACE. By default, they're both defined for you (although you can modify

this behavior using the Project Properties dialog box). The DEBUG constant

is defined to be True when you are compiling a Debug build of your program.

(You can choose what type of build you are making by selecting Build,

Configuration Manager from the menu bar and then choosing either the Debug

or Release version. Once you set your application to build as Release, the

Debug constant is set to False.) The TRACE constant allows you to control

runtime tracing of your application.

Declaring a Global Compiler Constant

To declare a compiler constant that can be used throughout your whole

application, select the project within the Solution Explorer window,

right-click, and select Properties from the context menu. On the Property

Pages dialog box, select Configuration Properties, Build. Figure 9.25 shows

the Property Pages dialog box.

Figure 9.25. Use this property page to set compile-time constants.

In the Custom Constants text box, enter the constant name, an equal sign,

and the value to assign. (Compile-time constants follow the same naming

rules as any other Visual Basic .NET variables.)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有