强制将数据类型传递给方法(严格的选项) [英] Enforce datatypes passed to methods (Option Strict)

查看:157
本文介绍了强制将数据类型传递给方法(严格的选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2005维护一些vb.net代码。我正在准备更新到较新的工具集,并首先进行代码清除。



我正在清除所有编译错误消息,但是我没有得到警告。如果我将String传递给需要整数的函数。



如何强制该警告?



具体情况是我正在更改此代码:

 公共函数MyFunc(ByVal MyVar)

 公共功能MyFunc (ByVal MyVar As Integer)

我想找到所有未传入Integer的地方。

解决方案

有选择地启用 Option Strict



添加到代码文件的顶部:

 
的选项严格

由于这只会在那些文件中强制执行此选项,因此不建议持续使用此方法,因为它需要



项目范围



打开项目属性 strong>(项目菜单-属性等)。在编译选项卡上,将 Option Strict 下拉菜单设置为打开。您还可以将某些条件从错误降级为警告,或者忽略该条件。



这也可以忘记。



Visual Studio默认(推荐)



您可以将 Option Strict On 设置为所有新项目的默认设置,因此您不必记住在任何级别进行操作:



工具菜单-> 项目和解决方案-> VB默认值



这不会影响已经在开发中的项目-为此使用#2。

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.

解决方案

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.

这篇关于强制将数据类型传递给方法(严格的选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆