DefaultValue problem

S

schneider

Hello,

Have an issue with a property using the DefaultValue(True) attribute.

Imports System.ComponentModel

Public Class Class1

Private m_testValue As Boolean

<DefaultValue(True)> _
Public Property test() As Boolean
Get
Return m_testValue
End Get
Set(ByVal value As Boolean)
m_testValue = value
End Set
End Property

End Class

here's the steps to reproduce the problem:

1. User UI sets the Class1.test=false during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=true during runtime.
4. I serialize Class1 to xml and the True value is NOT saved and the
False value is still there.


Step 4 is the problem, the file does not seem to reflect the current
state of the class.

If I remove the attribute it works fine, but the idea is the reduce the
file size and also know what the default is.

Anyone know what I'm doing wrong or missing?

Thanks,

Schneider
 
H

Herfried K. Wagner [MVP]

schneider said:
Have an issue with a property using the DefaultValue(True) attribute.
[...]
here's the steps to reproduce the problem:

1. User UI sets the Class1.test=false during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=true during runtime.
4. I serialize Class1 to xml and the True value is NOT saved and the False
value is still there.

Step 4 is the problem, the file does not seem to reflect the current state
of the class.

\\\
Imports System.ComponentModel
..
..
..
< _
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
_
DefaultValue(True) _
Public Property...
///
 
S

schneider

Herfried said:
schneider said:
Have an issue with a property using the DefaultValue(True) attribute.
[...]
here's the steps to reproduce the problem:

1. User UI sets the Class1.test=false during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=true during runtime.
4. I serialize Class1 to xml and the True value is NOT saved and the
False value is still there.

Step 4 is the problem, the file does not seem to reflect the current
state of the class.


\\\
Imports System.ComponentModel
.
.
.
< _

DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), _
DefaultValue(True) _

Public Property...
///

Does not seem to work, I include a more detailed sample (must be missing
something):

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim c As New Class1
c.test = False
Class1.SerializeObjectToFile("C:\test.txt", c)
c.test = True
Class1.SerializeObjectToFile("C:\test.txt", c)


Dim b As Class1
b = DirectCast(Class1.DeserializeObjectFromFile("C:\test.txt"), Class1)

Dim a As Integer
a = 1

End Sub

Imports System.ComponentModel
Imports System.IO

Public Class Class1

Private m_testValue As Boolean

<DefaultValue(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Property test() As Boolean
Get
Return m_testValue
End Get
Set(ByVal value As Boolean)
m_testValue = value
End Set
End Property

Public Shared Sub SerializeObjectToFile(ByVal fileName As String, ByVal
value As Object)

Dim writer As New StreamWriter(fileName)

Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serialization.XmlSerializer(value.GetType)

' Serialize the object, and close the StreamWriter.
s.Serialize(writer, value)

Catch
Throw
Finally
writer.Close()
End Try
End Sub

Public Shared Function DeserializeObjectFromFile(ByVal fileName As
String) As Object

Dim fs As New IO.FileStream(fileName, FileMode.Open)

Try
Dim w As New Xml.Serialization.XmlSerializer(GetType(Class1))
Dim g As Object = w.Deserialize(fs)

Return g

Catch
Throw
Finally
fs.Close()
End Try
End Function

End Class
 
D

Dustin Brisebois via DotNetMonster.com

Private m_testvalue as Boolean = True

Property TestValue() as Boolean
Get
return m_testvalue
End Get
Set(Value as Boolean)
m_testvalue = value
End Set

right?

Everytime the Class is redeclared m_testvalue is True by Default?
 
S

schneider

Dustin said:
Private m_testvalue as Boolean = True

Property TestValue() as Boolean
Get
return m_testvalue
End Get
Set(Value as Boolean)
m_testvalue = value
End Set

right?

Everytime the Class is redeclared m_testvalue is True by Default?

Thanks,

That was it.
Not initializing to Private m_testvalue as Boolean = True . Funny thing
is I do on others..

figures, something easy...

Schneider
 
H

Herfried K. Wagner [MVP]

schneider said:
That was it.
Not initializing to Private m_testvalue as Boolean = True . Funny thing is
I do on others..

Well, 'DefaultValueAttribute' doesn't set the property's value. MSDN:

| You can create a 'DefaultValueAttribute' with any value. A member's
| default value is typically its initial value. A visual designer can use
the
| default value to reset the member's value. Code generators can use
| the default values also to determine whether code should be generated
| for the member.
 

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