Weird type problem

E

Ewald Hofman

I have a very strange typing problem:

There is the following class

Public Class MyBaseClass
Implents MyInterface

Public Property MyProperty As MyClass implements MyInterface.MyProperty
...
End Class

I want to execute a method that uses the MyProperty from the MyBaseClass. So I made some tries to achieve this:

Try 1

public sub myMethod(argument as MyInterface)
dim MyObject as MyClass = argument.MyProperty
end sub

When I call the method I get the following exception: MissingMethodException (although the watch and immediate window gave me access to the property)

Try 2
When I define the method as follows (option strict is off):

public sub myMethod(argument as object)
dim MyObject as MyClass = CType(argument.MyProperty, MyClass)
end sub

I don't get the MissingMethodException anymore, but now I get the InvalidCastException (although I can perform the CType correctly in the watch and immediate window)

Try 3
When I define the method as follows (option strict is off):

public sub myMethod(argument as object)
dim MyObject as MyClass
If TypeOf argument.MyProperty Is MyClass Then
MyObject = CType(argument.MyProperty, MyClass)
End If
end sub

Now the CType is never performed and now there is the best of all: when I set the a breakpoint on the TypeOf line, then the watch window tells me the expression is true, but in the code window the expression is false, because it doesn't perform the CType.

Does anyone know what to do ????


Thanks in advance


Ewald Hofman
 
A

Armin Zingler

Ewald Hofman said:
When I define the method as follows (option strict is off):

First switch on option strcit. Otherwise it can be hard to find an error.
 
E

Ewald Hofman

That makes no difference.

Design-time (or compile time) everything is ok. It goes wrong when I'm in
run-time. The description in the MissingMethodException is 'dynamically
binding', but I'm not, because the complete code is based on early-binding.
 
H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
First switch on option strcit. Otherwise it can be hard to find an error.

You forgot:

Then switch off the HTML mode in your newsreader.

;-)
 
A

Armin Zingler

Herfried K. Wagner said:
You forgot:

Then switch off the HTML mode in your newsreader.

;-)

As long as HTML mode is switched off in _my_ news_reader_, I don't care
whether _his_ news_writer_ uses HTML mode.

;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top