Object By Value

VB6 Rooolz! (Still!)

Something I found handy in VB.--AndyMorris

If you want to pass objects around between components running in different processes or on different machines.

Have a property called State that contains all the internal data of the class in a string. You can then pass the object to (say) a persistence component like so.

  MyDataServer.SaveWidget MyWidget.State
MyDataServer would have a method that goes like

  Public Sub SaveWidget(WidgetState as String) 
  Dim theWidget as Widget
  Set theWidget = new  Widget
  Thewidget.State =  WidgetState
  ‘Now save the widget to your database or whatever
  End Sub 
Some ways of getting the state can be


I'm currently supporting a "Contents" property with Variant type -- using the MementoPattern. You can get or set the "Contents" -- which might be a String (maybe XML), or a Collection, or anything else. Later, you create a new object and initialize it's Contents with the data you got earlier.

I think Contents is a better name than state, thanks.

Of course, you have to trust the callers to not muck with your data -- which can be a big problem with certain PointyHairedBosses. ;-> -- JeffGrigg

Property bags can't be messed around with unless you know the names, on the other hand you can't read them easily when debugging.

Q: Do PropertyBags? automatically marshal across DCOM connections?

I've done it (between com components on different machines) and it works OK. If you think about it you can stuff a property bag .contents byte array into a file and cone back later so it must be OK.


In COM (i.e. VB < .NET) this is usually done by implementing IMarshal and supplying your own marshalling behavior. Of course... you can't implement IMarshal in VB, but there are implementations out there (see [1]) that will marshal a VB object by value as long as you implement the VB property bag persistance (i.e. IPersistStream[Init] in COM).

[1] http://www.develop.com/dbox/com/mbvref/

--DrewMarsh

Is this similar to setting the persistable property in the class properties to true?

Yes that's right.

That makes life easy but is only applicable to publicly creatable classes.

This is also right, unfortunately. VB never ceases to amaze does it?

CategoryVisualBasic


EditText of this page (last edited September 22, 2004) or FindPage with title or text search