Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deserialize - Ignore Case on Enum Type

Status
Not open for further replies.

Wholsea

Programmer
Jun 16, 2004
138
US
We have this enumerated type:

Public Enum RespCode
Info
Warn
[Error]
End Enum

This works great with most of the XML messages we get from our host, the problem is, sometimes we get a message with a RespCode of "ERROR" the serializer blows up because, obviously, that value isn't part of the enumerated type.

Any way we can get the deserializer to ignore the case on elements being stuffed into this enumarted type?

I could easily just replace this with a string value and let it ride, but we use this enumerated type is a TON of other code and can't afford a giant re-write at this point...

Please help...

ASP, VB, VBS, Cold Fusion, SQL, DTS, T-SQL, PL-SQL, IBM-MQ, Crystal Reports, Crystal Enterprise
 
Maybe something like this:

Code:
Public Class Form1

	Public Enum RespCode
		Info
		Warn
		[Error]
	End Enum

	Public RespCodeNames() As String = [Enum].GetNames(GetType(RespCode))

	Public Function GetRespCodeNameIndex(ByVal rc As String) As Integer

		Dim arr(RespCodeNames.Length - 1) As String
		For a As Integer = 0 To RespCodeNames.Length - 1
			arr(a) = RespCodeNames(a).ToLower
		Next
		Return Array.IndexOf(arr, rc.ToLower)

	End Function

	Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim rc As String = "ERROR"
		Dim ndx As Integer = GetRespCodeNameIndex(rc)
		If ndx > -1 Then
			rc = RespCodeNames(ndx)
			Dim x As RespCode = CType([Enum].Parse(GetType(RespCode), rc), RespCode)

			Select Case x
				Case RespCode.Error
					MessageBox.Show([Enum].GetName(GetType(RespCode), x))
				Case RespCode.Info
					MessageBox.Show([Enum].GetName(GetType(RespCode), x))
				Case RespCode.Warn
					MessageBox.Show([Enum].GetName(GetType(RespCode), x))
			End Select
		Else
			MessageBox.Show("Invalid Input")
		End If

	End Sub
End Class

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top