Monday, 24 November 2014

Object deserialized from XML has default values for some of the fields



I have small issue with deserializing the object of type ReaderAttributes. As you can see, internally it uses OnOff class to set some of the fields to be "ON" or "OFF"


After the object of ReaderAttributes deserialized, all the fields except OnOff are correctly set from XML file, rest are just set to false. I ran through the debugger, to see how ReaderAttributes is being constructed, and noticed that at very end of Sub New() it runs through the Public Sub New() of the OnOff eight times (number of times).



<Serializable, XmlRoot("ReaderAttributes")>
Public Class ReaderAttributes
' <TTY>OFF</TTY>
Public tty As OnOff
' <ECHO>OFF</ECHO>
Public echo As OnOff
' <BAUD>115200</BAUD>
Public baud As String
' <XONXOFF>OFF</XONXOFF>
Public xonxoff As OnOff
' <FIELDSEP>" "</FIELDSEP>
Public fieldsep As String
' <IDREPORT>OFF</IDREPORT>
Public idreport As OnOff
' <NOTAGRPT>OFF</NOTAGRPT>
Public notagrpt As OnOff
' <CHECKSUM>OFF</CHECKSUM>
Public checksum As OnOff
' <IDTRIES>3</IDTRIES>
Public idtries As Integer
' <RDTRIES>3</RDTRIES>
Public rdtries As Integer
' <WRTRIES>3</WRTRIES>
Public wrtries As Integer
' <SELTRIES>1</SELTRIES>
Public seltries As Integer
' <UNSELTRIES>1</UNSELTRIES>
Public unseltries As Integer
' <LOCKTRIES>1</LOCKTRIES>
Public locktries As Integer
' <INITTRIES>1</INITTRIES>
Public inittries As Integer
' <ANTTRIES>1</ANTTRIES>
Public anttries As Integer
' <BRI>ON</BRI>
Public bri As OnOff
' <ANTS>1,2,0,0,0,0,0,0</ANTS>
Public ants As String
' <TAGTYPE>G1</TAGTYPE>
Public tagtype As String
' <EPCIDREPORT>OFF</EPCIDREPORT>
Public epcidreport As OnOff
' <SWTTADDR>18</SWTTADDR>
Public swttaddr As Integer
' <SWTTLEN>1</SWTTLEN>
Public swttlen As Integer
' <POWER>100</POWER>
Public fieldstrength As String
Public Sub New()
tty = New OnOff()
echo = New OnOff()
baud = "115200"
xonxoff = New OnOff()
fieldsep = " "

idreport = New OnOff(True)
idreport.onoff = True

notagrpt = New OnOff()
checksum = New OnOff()
idtries = 3
rdtries = 3
wrtries = 3
seltries = 1
unseltries = 1
locktries = 1
inittries = 1
anttries = 1
bri = New OnOff()
bri.onoff = True
ants = "1"
tagtype = "EPCC1G2"
epcidreport = New OnOff()
swttaddr = 12
swttlen = 6
fieldstrength = "100,100,100,100"
End Sub
End Class

Public Class OnOff
Public onoff As Boolean
Public Sub New()
onoff = False
End Sub
Public Sub New(ByVal val As Boolean)
onoff = val
End Sub
Public Overrides Function ToString() As String
If Me.onoff Then
Return "ON"
Else
Return "OFF"
End If
End Function
End Class

No comments:

Post a Comment