Results 1 to 12 of 12

Thread: [RESOLVED] [2005] persistence properties

  1. #1

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Resolved [RESOLVED] [2005] persistence properties

    Hi guys...

    im trying to implement a version manager in my base form. For that i made an object that inherits a collection... its working fine except the data entered is not saved... how to persist properties data ?

    this is my code
    VB Code:
    1. Imports System.Collections
    2.  
    3. Public Class Base
    4.  
    5.     ''' <summary>
    6.     ''' Gets or sets the version manager.
    7.     ''' </summary>
    8.     ''' <value>The version manager.</value>
    9.     Public Property VersionManager() As Versions
    10.         Get
    11.             VersionManager = mVersionManager
    12.         End Get
    13.         Set(ByVal value As Versions)
    14.             mVersionManager = value
    15.         End Set
    16.     End Property
    17.     Dim mVersionManager As Versions
    18.  
    19. End Class

    VB Code:
    1. Public Class Version
    2.  
    3.     ''' <summary>
    4.     ''' Gets or sets the form major version.
    5.     ''' </summary>
    6.     ''' <value>The form major version.</value>
    7.     Public Property FormMajorVersion() As Integer
    8.         Get
    9.             FormMajorVersion = mFormMajorVersion
    10.         End Get
    11.         Set(ByVal value As Integer)
    12.             mFormMajorVersion = value
    13.         End Set
    14.     End Property
    15.     Dim mFormMajorVersion As Integer
    16.  
    17.     ''' <summary>
    18.     ''' Gets or sets the form minor version.
    19.     ''' </summary>
    20.     ''' <value>The form minor version.</value>
    21.     Public Property FormMinorVersion() As Integer
    22.         Get
    23.             FormMinorVersion = mFormMinorVersion
    24.         End Get
    25.         Set(ByVal value As Integer)
    26.             mFormMinorVersion = value
    27.         End Set
    28.     End Property
    29.     Dim mFormMinorVersion As Integer
    30.  
    31.     ''' <summary>
    32.     ''' Gets or sets the username.
    33.     ''' </summary>
    34.     ''' <value>The username.</value>
    35.     Public Property Username() As String
    36.         Get
    37.             Username = mUsername
    38.         End Get
    39.         Set(ByVal value As String)
    40.             mUsername = value
    41.         End Set
    42.     End Property
    43.     Dim mUsername As String
    44.  
    45.     ''' <summary>
    46.     ''' Gets or sets the comments.
    47.     ''' </summary>
    48.     ''' <value>The comments.</value>
    49.     Public Property Comments() As String
    50.         Get
    51.             Comments = mComments
    52.         End Get
    53.         Set(ByVal value As String)
    54.             mComments = value
    55.         End Set
    56.     End Property
    57.     Dim mComments As String
    58.  
    59.     ''' <summary>
    60.     ''' Initializes a new instance of the <see cref="Version" /> class.
    61.     ''' </summary>
    62.     Public Sub New()
    63.     End Sub
    64.  
    65.     ''' <summary>
    66.     ''' Initializes a new instance of the <see cref="Version" /> class.
    67.     ''' </summary>
    68.     ''' <param name="majorVersion">The major version.</param>
    69.     ''' <param name="minorVersion">The minor version.</param>
    70.     ''' <param name="userName">Name of the user.</param>
    71.     ''' <param name="comments">The comments.</param>
    72.     Public Sub New(ByVal majorVersion As Integer, ByVal minorVersion As Integer, ByVal userName As String, Optional ByVal comments As String = Nothing)
    73.         mFormMajorVersion = majorVersion
    74.         mFormMinorVersion = minorVersion
    75.         mUsername = userName
    76.         mComments = comments
    77.     End Sub
    78.  
    79.     ''' <summary>
    80.     ''' Initializes a new instance of the <see cref="Version" /> class.
    81.     ''' </summary>
    82.     ''' <param name="e">The e.</param>
    83.     Public Sub New(ByVal e As Version)
    84.         mFormMajorVersion = e.FormMajorVersion
    85.         mFormMinorVersion = e.FormMinorVersion
    86.         mUsername = e.Username
    87.         mComments = e.Comments
    88.     End Sub
    89.  
    90. End Class

    VB Code:
    1. Public Class Versions
    2.     Inherits CollectionBase
    3.  
    4.     ''' <summary>
    5.     ''' Adds the specified e.
    6.     ''' </summary>
    7.     ''' <param name="e">The e.</param>
    8.     Public Sub add(ByVal e As Version)
    9.         List.Add(e)
    10.     End Sub
    11.  
    12.     ''' <summary>
    13.     ''' Deletes the specified e.
    14.     ''' </summary>
    15.     ''' <param name="e">The e.</param>
    16.     Public Sub delete(ByVal e As Version)
    17.         List.Remove(e)
    18.     End Sub
    19.  
    20.     ''' <summary>
    21.     ''' Deletes the specified index.
    22.     ''' </summary>
    23.     ''' <param name="index">The index.</param>
    24.     Public Sub delete(ByVal index As Integer)
    25.         If index < 0 Or index > List.Count - 1 Then
    26.             Throw New Exception("Invalid index to remove")
    27.         End If
    28.         List.RemoveAt(index)
    29.     End Sub
    30.  
    31.     ''' <summary>
    32.     ''' Gets the Item.
    33.     ''' </summary>
    34.     ''' <value>The search.</value>
    35.     Default Public Overridable ReadOnly Property Item(ByVal index As Integer) As Version
    36.         Get
    37.             Return CType(Me.List(index), Version)
    38.         End Get
    39.     End Property
    40.  
    41.     ''' <summary>
    42.     ''' Gets the search.
    43.     ''' </summary>
    44.     ''' <value>The search.</value>
    45.     Public ReadOnly Property Search(ByVal mjorVersion As Integer) As Version
    46.         Get
    47.             For Each e As Version In Me.List
    48.                 If e.FormMajorVersion = mjorVersion Then Return e
    49.             Next
    50.             Return Nothing
    51.         End Get
    52.     End Property
    53.  
    54. End Class

    thx in advance...
    Last edited by zuperman; Jan 22nd, 2007 at 07:17 PM. Reason: separate classes

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] persistence properties

    I'd look into the Serializable() attribute, to see if that is to your liking. It's mighty fast to implement, but a casual glance at your code suggests that it might be overkill, as you seem to be trying to persist only a handful of values. Serializable gives you a fast way to store/retrieve class maps (classes within classes within classes etc.), but for just a few items, other options exist, especially in 2005 with that MySettings, or whatever it is called.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    thx for the tip... i will google it and let you know what i find...

  4. #4

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    by the way... which way the items and subitems of a listview are saved in design time ?

  5. #5

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    i've added SerializeObject and DeserializeObject procedures... im able to serialize this objects at runtime...

    but what about design time...as in the items of a listview... ???

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] persistence properties

    In .NET 2.0 you should inherit System.Collections.ObjectModel.Collection(Of T) rather than CollectionBase.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    Quote Originally Posted by jmcilhinney
    In .NET 2.0 you should inherit System.Collections.ObjectModel.Collection(Of T) rather than CollectionBase.
    Thx for the tip jm
    but how to persist data at design time ?

    VB Code:
    1. <Serializable()> _
    2. Public Class Versions
    3.     Inherits System.Collections.ObjectModel.Collection(Of Version)
    4.  
    5.     ''' <summary>
    6.     ''' Inserts the item.
    7.     ''' </summary>
    8.     ''' <param name="index">The index.</param>
    9.     ''' <param name="newItem">The new item.</param>
    10.     Protected Overloads Sub InsertItem( _
    11.         ByVal index As Integer, ByVal newItem As Version)
    12.         MyBase.InsertItem(index, newItem)
    13.     End Sub
    14.  
    15.     ''' <summary>
    16.     ''' Sets the item.
    17.     ''' </summary>
    18.     ''' <param name="index">The index.</param>
    19.     ''' <param name="newItem">The new item.</param>
    20.     Protected Overloads Sub SetItem(ByVal index As Integer, _
    21.         ByVal newItem As Version)
    22.         Dim replaced As Version = Items(index)
    23.         MyBase.SetItem(index, newItem)
    24.     End Sub
    25.  
    26.     ''' <summary>
    27.     ''' Removes the element at the specified index of the <see cref="T:System.Collections.ObjectModel.Collection`1"></see>.
    28.     ''' </summary>
    29.     ''' <param name="index">The zero-based index of the element to remove.</param>
    30.     ''' <exception cref="T:System.ArgumentOutOfRangeException">index is less than zero.-or-index is equal to or greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"></see>.</exception>
    31.     Protected Overrides Sub RemoveItem(ByVal index As Integer)
    32.         Dim removedItem As Version = Items(index)
    33.         MyBase.RemoveItem(index)
    34.     End Sub
    35.  
    36.     ''' <summary>
    37.     ''' Removes all elements from the <see cref="T:System.Collections.ObjectModel.Collection`1"></see>.
    38.     ''' </summary>
    39.     Protected Overrides Sub ClearItems()
    40.         MyBase.ClearItems()
    41.     End Sub
    42. End Class

  8. #8

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    let me show you what i want
    Attached Images Attached Images  

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] persistence properties

    Wow, it doesn't persist automatically? I thought anything you did in design view was saved when you saved the project. I wasn't aware of anything that got reset.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    Quote Originally Posted by Shaggy Hiker
    Wow, it doesn't persist automatically? I thought anything you did in design view was saved when you saved the project. I wasn't aware of anything that got reset.
    or i am doing something wrong...

  11. #11

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [2005] persistence properties

    ok... i solved myself... so simple... i forgot to Initialize the collection property...
    VB Code:
    1. <System.Diagnostics.DebuggerStepThrough()> _
    2.     Private Sub InitializeComponent()
    3.         components = New System.ComponentModel.Container()
    4.         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    5.         Me.Text = "Form1"
    6.         Me.VersionManager = New Versions
    7.     End Sub

    Here it is the sample project...in case you face the same problem...

    one thing has to be implemented... serialize the Versions collection in the base finalize destructor and deserialize in the base constructor... or else you loose data when you close the form...

    thx for all your help guys...
    Attached Files Attached Files

  12. #12

    Thread Starter
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033

    Re: [RESOLVED] [2005] persistence properties

    Finally i made my windows forms version manager... works in design and run time...

    no need to insert version history in the code comments... just use this component in your base form...

    thx a lot for all the help...
    Attached Images Attached Images  
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width