[英]Why can I pass the wrong interface to a function without compiler errors?
Curious if I've messed up some Visual Studo settings that should be flagging this, or if I'm misunderstanding something entirely.好奇我是否弄乱了一些应该标记此问题的 Visual Studo 设置,或者我是否完全误解了某些东西。
I have a funcion A which has 2 very basic interfaces as parameters:我有一个函数 A,它有 2 个非常基本的接口作为参数:
Function B has a single parameter: Function B 有一个参数:
Function A calls Function B, passing oConfiguration as an argument. Function A 调用 Function B,将 oConfiguration 作为参数传递。 I don't seem to get any compiler errors or warnings in Visual Studio but of course I get the runtime error:我似乎在 Visual Studio 中没有收到任何编译器错误或警告,但我当然会收到运行时错误:
Unable to cast object of type 'ConfigurationImplementationA' to type 'IEventLogHandler'.无法将“ConfigurationImplementationA”类型的 object 转换为“IEventLogHandler”类型。
Turn on Option Strict .打开Option Strict 。
It's off by default to support legacy VB6 code.它默认关闭以支持旧版 VB6 代码。 Otherwise, it's just pain and suffering to leave it off, so don't.否则,离开它只是痛苦和痛苦,所以不要。
See also: What do Option Strict and Option Explicit do?另请参阅: Option Strict 和 Option Explicit 有什么作用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.