1

I've got a function in VB like the following:

Public Overrides Sub FnName(param1 As ISomeInterface)
    Dim TempVar As String = param1.prop1
End Sub

When I try to build the code in Visual Studio, I get the following error:

"prop1" is not a member of "param1".

However, when I add a breakpoint to the code and run the debugger, I can clearly see the param1 parameter under the Autos and Locals tabs, and when I expand it, I can see the prop1 property with a valid string value.

It should be noted that prop1 is not actually part of the ISomeInterface interface, so I'm thinking that the property is being dynamically added to the parameter variable after the fact. (I'm very new to the codebase in question.)

Long story short, how do I access the string value stored in param1.prop1 without getting an error?

I am relatively new to VB, but no amount of Googling various keywords has netted me a valid page/topic. Thank you.

HartleySan
  • 7,404
  • 14
  • 66
  • 119
  • 3
    "... prop1 is not actually part of the ISomeInterface interface" - then unless you use late-binding (high advise against this approach) you can not access the underlying type's properties without first casting to said type. This raises a question about the code's design though. If you need to access something not defined in `ISomeInterface` in the `FnName` method, why is the argument defined as `ISomeInterface`? – TnTinMn Dec 04 '18 at 21:04
  • TnTinMn, not disagreeing with you at all on the approach possibly not being ideal, but that's not something I have any say over at this time. I simply need to access `param1.prop1`, regardless of whether it's ideal or not. That being said, how would I access the property with either late binding or by casting it to the correct type? Thanks. – HartleySan Dec 04 '18 at 21:08
  • 1
    If you set Option Strict to Off in the Compile properties of the project, i'm guessing the code will likely build (and run). That's not the best idea for future maintenance of that code, but it may be how that code was originally written. – Jon E Dec 04 '18 at 21:08
  • `Option Strict On` is set and I cannot remove that. What should I do? Thanks. – HartleySan Dec 04 '18 at 21:10
  • 2
    `Dim variable As RealTypeName = TryCast(param1, RealTypeName) : If variable IsNot Nothing Then TempVar = variable.prop1`. However, it would be better to define a new method. `Public Sub FnName(param1 As RealTypeName)`. – TnTinMn Dec 04 '18 at 21:11
  • I agree with both of you, TnTinMn and Jon E. Unfortunately, I don't have that level of control with the code at this point. As such, I'll have to stick with your typecasting solution, TnTinMn. I'll explore further though and see if other changes can be made though. Thank you. – HartleySan Dec 04 '18 at 21:19
  • And just as an FYI, the typecasting solution worked perfectly for what I was trying to do. Thanks. – HartleySan Dec 04 '18 at 21:30

0 Answers0