473,549 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DefaultValue problem

Hello,

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

Imports System.Componen tModel

Public Class Class1

Private m_testValue As Boolean

<DefaultValue(T rue)> _
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=fal se during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=tru e 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 1429
"schneider" <ab*@123.com> schrieb:
Have an issue with a property using the DefaultValue(Tr ue) attribute.
[...]
here's the steps to reproduce the problem:

1. User UI sets the Class1.test=fal se during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=tru e 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.Componen tModel
..
..
..
< _
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible),
_
DefaultValue(Tr ue) _ _

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(Tr ue) attribute.
[...]
here's the steps to reproduce the problem:

1. User UI sets the Class1.test=fal se during runtime.
2. I serialize Class1 to xml and the false value is saved.
3. User UI sets the Class1.test=tru e 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.Componen tModel
.
.
.
< _

DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible), _
DefaultValue(Tr ue) _
_


Public Property...
///


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

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

Dim c As New Class1
c.test = False
Class1.Serializ eObjectToFile(" C:\test.txt", c)
c.test = True
Class1.Serializ eObjectToFile(" C:\test.txt", c)
Dim b As Class1
b = DirectCast(Clas s1.DeserializeO bjectFromFile(" C:\test.txt"), Class1)

Dim a As Integer
a = 1

End Sub

Imports System.Componen tModel
Imports System.IO

Public Class Class1

Private m_testValue As Boolean

<DefaultValue(T rue),
DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.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 SerializeObject ToFile(ByVal fileName As String, ByVal
value As Object)

Dim writer As New StreamWriter(fi leName)

Try
' Create a new XmlSerializer instance.
Dim s As New Xml.Serializati on.XmlSerialize r(value.GetType )

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

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

Public Shared Function DeserializeObje ctFromFile(ByVa l fileName As
String) As Object

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

Try
Dim w As New Xml.Serializati on.XmlSerialize r(GetType(Class 1))
Dim g As Object = w.Deserialize(f s)

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.c om 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, 'DefaultValueAt tribute' doesn't set the property's value. MSDN:

| You can create a 'DefaultValueAt tribute' 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
1881
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 dataset my controls don't have the defaultvalue entered in the database. Is it possible to retrieve the defaultvalue for the column from the...
1
1949
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 of code in the Form's AfterUpdate event? Thanks!
2
11440
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 ignores it! I´ve also tried other things like:
3
4192
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 null-assignments for the string properties, while I've specified that NULL is the default value. Other types work well. thanks in advance, Bram.
0
946
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 problem, however, occours in the following code at all. It's easy copy the following code for reproducing the problem. This is a class that simply...
3
1993
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 been entered there. First, I used Me!TowFee.DefaultValue = Null That returned an error, so I used Me!TowFee.DefaultValue = ""
10
3271
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 { return showDropDownButtons; }
6
9377
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") DefaultValue("bool","true")
25
5600
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 checking the overtime allowance. Here is the code. Private Sub OT_Test() If Forms!frmJobHourCostEmpName.TotalHours = 8 Then Me.OTJob.DefaultValue = True...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.