简体   繁体   中英

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.

I have a funcion A which has 2 very basic interfaces as parameters:

  1. oConfiguration as IConfiguration
  2. oEventLogHandler as IEventLogHandler

Function B has a single parameter:

  1. oEventLogHandler as IEventLogHandler

Function A calls Function B, passing oConfiguration as an argument. I don't seem to get any compiler errors or warnings in Visual Studio but of course I get the runtime error:

Unable to cast object of type 'ConfigurationImplementationA' to type 'IEventLogHandler'.

Turn on Option Strict .

It's off by default to support legacy VB6 code. Otherwise, it's just pain and suffering to leave it off, so don't.

See also: What do Option Strict and Option Explicit do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM