Click here to Skip to main content
6,595,444 members and growing! (22,059 online)
Email Password   helpLost your password?
Languages » VB.NET » Visual Basic .NET     Beginner License: The Code Project Open License (CPOL)

PropertyBag in VB .NET

By kiruba kumaresh R

This article gives an overview of the propertybag class implemented in VB.NET.
VB, Windows, .NET (.NET 2.0), Dev
Posted:11 Jan 2008
Views:8,867
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.16 Rating: 1.92 out of 5
2 votes, 50.0%
1

2
1 vote, 25.0%
3

4
1 vote, 25.0%
5

Introduction

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.

WriteProperty

Definition

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

Description

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

ReadProperty

Definition

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.

Description

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

Contents

Definition

Public Property Contents()

Description

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

Clear

Definition

Public Sub Clear()

Description

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.
    pbag.WriteProperty("iInputNum",1)
  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(bytearr)
         // 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

License

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


Member
I have completed my undergraduate degree in IT in 2004 and working as a software developer since then.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralPropertyBag PinmemberSr.Perkin0:58 14 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jan 2008
Editor: Deeksha Shenoy
Copyright 2008 by kiruba kumaresh R
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project