Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

.net - Enforce datatypes passed to methods (Option Strict)

I am using VS2005 to maintain some vb.net code. I am preparing to update to newer toolsets and am doing code cleanup first.

I am cleaning up all the compile error messages, but I noticed that I do not get a warning if I pass a String to a function that is expecting an Integer.

How can I force that warning?

The specific situation is that I am changing this code:

Public Function MyFunc(ByVal MyVar)

to

Public Function MyFunc(ByVal MyVar As Integer)

And I want to find all the places that were NOT passing in an Integer.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Selectively Turn on Option Strict

Add to the top of the code file:

Option Strict On

Since this will enforce the option only in those files, this method is not recommended on an ongoing basis because it requires you to remember to do so for each file.

Project Wide

Open Project Properties (Project Menu - Properties among others). On the Compile Tab set the Option Strict drop down to On. You can also demote some conditions from an error to a warning or ignore that condition.

This too can be forgotten.

Visual Studio Default (recommended)

You can make Option Strict On the default setting for all new projects, so you don't have to remember to do so at any level:

Tools Menu -> Projects and Solutions -> VB Defaults

This wont affect a project already in the works - use #2 for that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...