I learned a new thing today: static local variables (a feature inherent to VB.NET) and serialization don't mix.
Here's what I did that did not work. I have a class whose objects can expose themselves as objects of a different class. Because I am not familiar with any design pattern to do this (maybe the actor-role pattern (or role pattern for short) will do this?), I came up with this solution:
Public Readonly Property StringAsSentence As Sentence
Get
Static RetVal As New Sentence
' More code here to initialize the sentence
Return Sentence
End Get
End Property
Now, it turns out that the way the VB.NET compiler gets around the Framework's inability to use static local variables is by actually declaring that local variable as a class level Shared (static in C#) variable. Patrick Steele has a good explanation of this process.
The problem seems to be that although the MSDN document states that StaticLocalInitFlag is Serializable, it is not. Therefore, while trying to invoke a remote method that uses an object of the class that defined the above method as a parameter, I get the following error message:
The type Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag in Assembly Microsoft.VisualBasic, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=<snip> is not marked as serializable.
I couldn't actually find this exception message anywhere on the Internet, so I was a bit at a loss at first. Also, in the documentation for .NET 1.1, the StaticLocalInitFlag class is not documented. (The URL above refers to the documentation for .NET 2.0, but states that it applies to .NET 1.1 also.)
Here's how I tried to solve the problem at first. I tried to change the property into a function, thinking that the Framework wouldn't try to serialize this no longer. Unfortunately, I wasn't paying attention to what I had just read in Patrick Steele's blog, because as long as that local static variable exists, it will turn into a Shared member variable of the class whose objects needs serializing. It continued to fail. So I just removed the Static declaration and the problem was solved.