I'm consuming a 3rd party web service in a .Net (3.5) project (imported via "Service References") and unfortunately in their wisdom they've created an enum that has case sensitive name collisions, eg.
// This is the C# generated from the wsdl
public enum SIunitType {
// snip
l,
Nm,
rpm,
m3,
L,
// snip
}
// Pre populate a pair of enums so that we can easily query them in VB
public class Class1 {
public static SIunitType small_l = SIunitType.l;
public static SIunitType big_l = SIunitType.L;
}
' VB method
public class Class1 {
Public Shared Sub Foo()
Dim i1 As Integer = CInt(Class1.small_l) ' This gives 10
Dim i2 As Integer = CInt(Class1.big_l) ' This gives 18
Dim b As Boolean = (Class1.small_l = Class1.big_l) ' This is false
Dim isBig As Boolean
Select Case Class1.big_l
Case SIunitType.l
' Never get here
isBig = False
Case CType([Enum].Parse(GetType(SIunitType), "L", False), SIunitType)
' So, parsing the case sensitive value works, is this the only way?
isBig = True
End Select
End Sub
As you can see, there as to "l"s. This is fine if we're using C# or any other case sensitive language. However, when the library is imported into a VB.net project we quite obviously get some problems: The intellisense corrected the "L" to an "l", probably as it's defined first. When debugging, querying a enum value returned by the library shows that there is a difference, however there's not way to test it, apart from using magic numbers (which defeats the purpose of using an enum), or reflection.
A couple of points:
- The obvious answer is "don't use names that can cause this issue, however I don't have that option - it's a 3rd party.
- Getting the 3rd party to change this isn't going to happen - this is a web service provided to companies across Europe.
- Unfortunately I'm stuck using VB.Net for part of the project, so "use a better language" isn't an option.
So, can anyone suggest a better of way dealing with this?
Y
andy
in an enum. Luckily the two values mean the same and it's not difficult to fix the generated service mapping file by deleting one of them. – GSerg Oct 9 '15 at 16:18