vb.net Option Strict On 不允许后期绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12375405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Option Strict On disallows late binding
提问by mike
Can someone help me fix this error?
有人可以帮我解决这个错误吗?
Option Strict On disallows late binding
Option Strict On 不允许后期绑定
Here's the code that's causing the error:
这是导致错误的代码:
Dim SF6StdData As BindingSource = New BindingSource()
' ...
If StrComp(SF6StdData.Current("O2AreaCts").ToString, "") = 0 Then
AreaCts(3) = 0
Else
AreaCts(3) = Convert.ToDouble(SF6StdData.Current("O2AreaCts").ToString)
End If
I need to rewrite the code so it will not have any errors. I know I could fix this by setting Option Strict to Off in the project properties, but I really don't want to do that. Is there some other way?
我需要重写代码,这样它就不会出现任何错误。我知道我可以通过在项目属性中将 Option Strict 设置为 Off 来解决这个问题,但我真的不想这样做。还有其他方法吗?
回答by Steven Doggart
Late binding is not allowed when Option Strict
is on. If you need to perform late binding, the only options are either to use reflection or to shut off Option Strict
. The one saving grace, however, is that you don't have to shut off Option Strict
for the whole project. You can leave it on for the project and then just add the line Option Strict Off
at the top of any code files where you need to perform late binding. It's not a great solution, but it's better than affecting the whole project.
Option Strict
打开时不允许延迟绑定。如果您需要执行后期绑定,唯一的选择是使用反射或关闭Option Strict
. 然而,一个可取之处是您不必关闭Option Strict
整个项目。您可以为项目保留它,然后只需Option Strict Off
在需要执行后期绑定的任何代码文件的顶部添加该行。这不是一个很好的解决方案,但总比影响整个项目要好。
Also, since the Option Strict
placed at the top of a file applies just to that file, it doesn't even have to apply to an entire class. If you split your class into multiple Partial Class
files, then you could have Option Strict
set differently for each of those files. For instance, if you put most of your class in a file with Options Strict On
, and then just put one method in a Partial Class
in a separate file with Option Strict Off
, then only that one method would be compiled loosely. The rest of the class would be compiled using the strict rules.
此外,由于Option Strict
放置在文件顶部的 仅适用于该文件,因此它甚至不必适用于整个类。如果您将类拆分为多个Partial Class
文件,那么您可以Option Strict
为每个文件设置不同的设置。例如,如果您将大部分类放在一个文件中Options Strict On
,然后只将一个方法Partial Class
放在一个单独的文件中Option Strict Off
,那么只有一个方法会被松散地编译。类的其余部分将使用严格的规则进行编译。
回答by Andrew Morton
You need to make the BindingSource act as a strongly-typed data source. See the remarks in the documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx
您需要使 BindingSource 充当强类型数据源。请参阅文档中的备注:http: //msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx
回答by kadzema
If you declared AreaCts
without a type, ex:
如果你声明AreaCts
没有类型,例如:
Dim AreaCts as Array
Try
尝试
Dim AreaCts() as Double
This fixed my late binding error.
这修复了我的后期绑定错误。