DefaultValue problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • schneider

    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
  • Herfried K. Wagner [MVP]

    #2
    Re: DefaultValue problem

    "schneider" <abd@123.com> schrieb:[color=blue]
    > 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.[/color]

    \\\
    Imports System.Componen tModel
    ..
    ..
    ..
    < _
    DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible),
    _
    DefaultValue(Tr ue) _[color=blue]
    > _[/color]
    Public Property...
    ///

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

    Comment

    • schneider

      #3
      Re: DefaultValue problem

      Herfried K. Wagner [MVP] wrote:[color=blue]
      > "schneider" <abd@123.com> schrieb:
      >[color=green]
      >> 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.[/color]
      >
      >
      > \\\
      > Imports System.Componen tModel
      > .
      > .
      > .
      > < _
      >
      > DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Visible), _
      > DefaultValue(Tr ue) _
      >[color=green]
      >> _[/color]
      >
      > Public Property...
      > ///
      >[/color]

      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


      Comment

      • Dustin Brisebois via DotNetMonster.com

        #4
        Re: DefaultValue problem

        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

        Comment

        • schneider

          #5
          Re: DefaultValue problem

          Dustin Brisebois via DotNetMonster.c om wrote:[color=blue]
          > 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?
          >[/color]

          Thanks,

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

          figures, something easy...

          Schneider

          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: DefaultValue problem

            "schneider" <eschneider100@ wi.rr.com> schrieb:[color=blue][color=green]
            >> Everytime the Class is redeclared m_testvalue is True by Default?[/color]
            >
            > That was it.
            > Not initializing to Private m_testvalue as Boolean = True . Funny thing is
            > I do on others..[/color]

            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/>

            Comment

            Working...