473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1417
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Maurice Mertens | last post by:
Hi, I have an Access database in which I defined default values for certain columns. On my windows forms I've got controls bound to the dataset, The problem is that when I add a new row to the...
1
by: Mark | last post by:
How do I write the following code to set the default value? Me!xNum.DefaultValue = Me!XCurrency.DefaultValue = Me!XDate.DefaultValue = Me!XString.DefaultValue = Can I just put all the lines...
2
by: timtos | last post by:
I want to set a color property of a user control by default. I am doing it like this: .... DefaultValue(typeof(System.Drawing.Color), "Blue"), .... The compiler likes it but my program...
3
by: Bram Vandendriesschen | last post by:
Hi, anyone an idea how to get DefaultValue's working for String properties in managed C++. I wrote a component in managed C++ which I plan to consume in C#. Now the IDE keeps inserting...
0
by: Carlo, MCP | last post by:
Good morning, sorry for the lenght of the code, but it's six months that I've this problem. The following code is an extreme simplification of the original code, that is much more complex. The...
3
by: MLH | last post by:
On frmVehicleEntryForm... I wanted to change the DefaultValue property of one of my textbox controls on a form from within code. I wanted the setting to be no setting - as if nothing had ever...
10
by: tony | last post by:
Hello!! I have some demo programs written in C# and they have this construction "" see below. I haven't seen this before so what does it mean ? public bool ShowDropDownButtons { get {...
6
by: Peter Hartlén | last post by:
Hi! What's the correct syntax for the default value design time attribute, using classdiagram view and Custom Attributes dialog. For a boolean: DefaultValue(true) DefaultValue("true")...
25
by: kstevens | last post by:
I have a sub to determine if overtime has come into play yet on a certain job. If (preapproved) overtime has started then the next (subform) record can automatically be populated from a query...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.