if Typeof ... fails

Grimalkyne

Member
Joined
Feb 21, 2005
Messages
7
Programming Experience
5-10
I have a function that takes a generic interface ISettings.

I want to treat it differently depending on a more specific type so I use

if typeof x is CSettings then
HandleCSettings(x)
else if typeof x is DSettings then
HandleDSettings(x)
else
HandleDefaultSettings(x)
end if

When I pass in a DSettings object, the tool tip on "typeof x is DSettings" is true but it skips over that block and goes into the HandleDefaultSettings code.

I have recompiled the code, so it's not the view out of synch with the underlying code.

Does anyone have any suggestions?
Thanks
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,561
Location
Norway
Programming Experience
10+
So basically you have this code:
VB.NET:
Interface ISettings
End Interface

Class DSettings : Implements ISettings
End Class
and when you run this code you claim stop is B:
VB.NET:
Dim x As ISettings = New DSettings
If TypeOf x Is DSettings Then
    Stop 'A
Else
    Stop 'B
End If
When compiled this code will stop at A as expected (you can paste this code into your project and run it as posted), so if you are getting different result your code can't be compiled. One reason for this can be that the generated temp files VS puts in Obj folder has corrupted and that the compiled output in Bin folder is left unchanged. I saw this happen last time in VS 2003 some years ago, and the remedy was to exit VS and delete the Obj and Bin folders, when they were generated again the compile process started working again.
 

Grimalkyne

Member
Joined
Feb 21, 2005
Messages
7
Programming Experience
5-10
Thanks for that

I am using VS 2003 on some legacy code. I did as you suggested and it worked but ...

I got clean code out and recompiled it and got the same error back.
This time deleting the obj and bin folders hasn't resolved the problem.

Any further thoughts?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,561
Location
Norway
Programming Experience
10+
I think you perhaps misunderstood my comment about the build process and VS 2003, this is something that can only happen if you make changes to code and the changes is not reflected in runtime, it is not something that will make code behave differently from one build to another. I find it hard to believe that your system stops at B when compiling the code sample I posted. That code sample was intended as a complete repro of the case scenario you described in first post, if it doesn't fit the description you'll have to explain further.
 

Grimalkyne

Member
Joined
Feb 21, 2005
Messages
7
Programming Experience
5-10
I finally uncovered the cause; I had a refence to a project set to copy-local.
For some reason it was changing the signature of the copied dll which was causing the
typeof comparison to return false when it should have returned true

JohnH
thanks for your help and input.
 
Top Bottom