By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Serailizing Static Variables Problems

P: n/a
I'm trying to serialize a class using the binary formatter without serializing the
'static pastFirstCall as Boolean' variable. Normally private (module-scope)
variables are serialized unless the <NonSerialized> attribute is used.

Public Function NextMessageNo() As Integer
<NonSerialized()> Static pastFirstCall As Boolean = False 'This is
being Serialized!!!!!!!!!!
If Not pastFirstCall Then
mNextMessageNumber = mNextPersistableMessageNo
pastFirstCall = True
End If
NextMessageNo = mNextMessageNumber + 1
mNextMessageNumber = NextMessageNo
End Function
<NonSerialized()> Private mNextMessageNumber As Integer
Using Reflector, I can see that, internally, pastFirstCall is declared as a private
variable as:
Private $STATIC$NextMessageNo$2008$pastFirstCall As BooleanThe problem is that the
<NonSerialized> attribute is not carried forward to this declaration. Are there any
workarounds other than changing the delcaration to private. Has anyone written a
custom attribute to fix it? I have a ton of static's (which I like because they
limit the scope, thus reducing the complexity) and don't want to go thru all my code
changing them to private module-scope declarations. Almost forgot, I'm using the
..NET Framework 1.0 with the latest service packs. Any Ideas?

Thanks - JackRazz

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jack,

There really isn't a way to get around this, you will have to move this
to the module level as there is no way to declare the attribute properly on
the variable when it is static in a method call.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JackRazz" <Ja******@NotValid.com> wrote in message
news:e8**************@TK2MSFTNGP10.phx.gbl...
I'm trying to serialize a class using the binary formatter without
serializing the
'static pastFirstCall as Boolean' variable. Normally private
(module-scope)
variables are serialized unless the <NonSerialized> attribute is used.

Public Function NextMessageNo() As Integer
<NonSerialized()> Static pastFirstCall As Boolean = False
'This is
being Serialized!!!!!!!!!!
If Not pastFirstCall Then
mNextMessageNumber = mNextPersistableMessageNo
pastFirstCall = True
End If
NextMessageNo = mNextMessageNumber + 1
mNextMessageNumber = NextMessageNo
End Function
<NonSerialized()> Private mNextMessageNumber As Integer
Using Reflector, I can see that, internally, pastFirstCall is declared as
a private
variable as:
Private $STATIC$NextMessageNo$2008$pastFirstCall As BooleanThe problem is
that the
<NonSerialized> attribute is not carried forward to this declaration. Are
there any
workarounds other than changing the delcaration to private. Has anyone
written a
custom attribute to fix it? I have a ton of static's (which I like
because they
limit the scope, thus reducing the complexity) and don't want to go thru
all my code
changing them to private module-scope declarations. Almost forgot, I'm
using the
.NET Framework 1.0 with the latest service packs. Any Ideas?

Thanks - JackRazz

Nov 21 '05 #2

P: n/a
Nicholas,
Thanks for the info. Did they ever fix it with the 1.1 framework?


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
| Jack,
|
| There really isn't a way to get around this, you will have to move this
| to the module level as there is no way to declare the attribute properly on
| the variable when it is static in a method call.
|
| Hope this helps.
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - mv*@spam.guard.caspershouse.com
|
| "JackRazz" <Ja******@NotValid.com> wrote in message
| news:e8**************@TK2MSFTNGP10.phx.gbl...
| > I'm trying to serialize a class using the binary formatter without
| > serializing the
| > 'static pastFirstCall as Boolean' variable. Normally private
| > (module-scope)
| > variables are serialized unless the <NonSerialized> attribute is used.
| >
| > Public Function NextMessageNo() As Integer
| > <NonSerialized()> Static pastFirstCall As Boolean = False
| > 'This is
| > being Serialized!!!!!!!!!!
| > If Not pastFirstCall Then
| > mNextMessageNumber = mNextPersistableMessageNo
| > pastFirstCall = True
| > End If
| > NextMessageNo = mNextMessageNumber + 1
| > mNextMessageNumber = NextMessageNo
| > End Function
| > <NonSerialized()> Private mNextMessageNumber As Integer
| >
| >
| > Using Reflector, I can see that, internally, pastFirstCall is declared as
| > a private
| > variable as:
| > Private $STATIC$NextMessageNo$2008$pastFirstCall As BooleanThe problem is
| > that the
| > <NonSerialized> attribute is not carried forward to this declaration. Are
| > there any
| > workarounds other than changing the delcaration to private. Has anyone
| > written a
| > custom attribute to fix it? I have a ton of static's (which I like
| > because they
| > limit the scope, thus reducing the complexity) and don't want to go thru
| > all my code
| > changing them to private module-scope declarations. Almost forgot, I'm
| > using the
| > .NET Framework 1.0 with the latest service packs. Any Ideas?
| >
| > Thanks - JackRazz
| >
| >
| >
|
|
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.