473,320 Members | 1,691 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,320 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 1414
"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.