471,616 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

DefaultValue problem

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
Nov 21 '05 #1
5 1333
"schneider" <ab*@123.com> schrieb:
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(DesignerSerializat ionVisibility.Visible),
_
DefaultValue(True) _ _

Public Property...
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
Herfried K. Wagner [MVP] wrote:
"schneider" <ab*@123.com> schrieb:
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(DesignerSerializat ionVisibility.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:\te st.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(DesignerSerializat ionVisibility.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
Nov 21 '05 #3
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?

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #4
Dustin Brisebois via DotNetMonster.com wrote:
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
Nov 21 '05 #5
"schneider" <es***********@wi.rr.com> schrieb:
Everytime the Class is redeclared m_testvalue is True by Default?


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Maurice Mertens | last post: by
2 posts views Thread by timtos | last post: by
3 posts views Thread by Bram Vandendriesschen | last post: by
6 posts views Thread by Peter Hartlén | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.