Click here to Skip to main content
13,250,043 members (70,231 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 11 Jan 2008

PropertyBag in VB .NET

, 11 Jan 2008
Rate this:
Please Sign up or sign in to vote.
This article gives an overview of the propertybag class implemented in VB.NET.


This article gives an overview of the propertybag class implemented in VB.NET.

Basically Propertybag is a virtual container which can hold any type of data and can be used to persist objects between instances. This class simulates the behaviour of propertybag supported in VB 6.0.

PropertyBag Class

The PropertyBag class exposes the following methods/properties which can be used to add/access the objects in it.



Public Sub WriteProperty(ByVal szobjDesc As String, ByVal objValue As Object)
  • szObjDesc: Description of the object
  • objValue: Object


Writes the given object to the bag with the specified description.



Public Function ReadProperty(ByVal szobjDesc As String, _
    Optional ByVal ObjDefValue As Object = Nothing) As Object
  • szObjDesc: Description of the object
  • ObjDefValue: Default value to be returned if the object was not found in the bag. Defaults to nothing if not specified.


Returns the specified object from the bag. If the object is not found, default value specified in the method call will be returned.



Public Property Contents()


Gets or sets the Contents of the bag.

  • Get: Returns the Contents of the bag as a byte array (in binary serialized format)
  • Set: Sets the Contents of the bag with the given byte array



Public Sub Clear()


Clears the contents of the bag.

Using the Code

Here are the steps to be followed to access the PropertyBag in your application:

  1. Add a reference to the PropertyBag Assembly in your application. The PropertyBag class becomes visible now.
  2. Instantiate the Object:
    Dim pbag as New PropertyBag()
  3. Add objects to the bag with description (which will be used later to retrieve the objects from the bag). Only objects which are serializable should be added to the bag, or else an exception will be thrown when you try to retrieve the contents of the bag using the contents property.
  4. Read the objects from the bag when needed:
    Dim iValue as integer
        iValue = pbag.ReadProperty("iInputNum")
  5. In case you want to send the data in Propertybag across processes, serialize it to binary format and send the data:
    Dim bytearr() as byte
         bytearr= pbag.Contents
         // RemoteSub which runs in separate process
         Public sub RemoteSub(bytearr() as byte)
         Dim pbag as New Propertybag
         Dim iValue as integer
         pbag.contents = bytearr
         iValue = pbag.ReadProperty("iInputNum")
     End Sub


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

kiruba kumaresh R
Software Developer (Senior)
United States United States
I have completed my undergraduate degree in IT in 2004 and working as a software developer since then.

You may also be interested in...

Comments and Discussions

QuestionWhat this code does Pin
Member 104061843-Feb-15 13:22
memberMember 104061843-Feb-15 13:22 
QuestionThanks! Pin
DBS0611-Sep-12 0:34
memberDBS0611-Sep-12 0:34 
QuestionData does not persist in EXE Pin
guideX14-Sep-11 19:47
memberguideX14-Sep-11 19:47 
I thought the point of the property bag was you could save the info in the property bag directly in the exe, and recall it on launch, eliminating the need to load some external file with just that exe, stop me if I'm wrong..
QuestionToo many arguments ERROR Pin
praveen52423-Mar-10 4:35
memberpraveen52423-Mar-10 4:35 
AnswerRe: Too many arguments ERROR Pin
_Plutonix16-Oct-11 15:50
member_Plutonix16-Oct-11 15:50 
GeneralThanks for the sample code Pin
aschoepske17-Dec-09 11:31
memberaschoepske17-Dec-09 11:31 
GeneralPropertyBag Pin
Sr.Perkin14-Jul-08 0:58
memberSr.Perkin14-Jul-08 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 11 Jan 2008
Article Copyright 2008 by kiruba kumaresh R
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid