473,811 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serailizing Static Variables Problems

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 <NonSerialize d> attribute is used.

Public Function NextMessageNo() As Integer
<NonSerialized( )> Static pastFirstCall As Boolean = False 'This is
being Serialized!!!!! !!!!!
If Not pastFirstCall Then
mNextMessageNum ber = mNextPersistabl eMessageNo
pastFirstCall = True
End If
NextMessageNo = mNextMessageNum ber + 1
mNextMessageNum ber = NextMessageNo
End Function
<NonSerialized( )> Private mNextMessageNum ber As Integer
Using Reflector, I can see that, internally, pastFirstCall is declared as a private
variable as:
Private $STATIC$NextMes sageNo$2008$pas tFirstCall As BooleanThe problem is that the
<NonSerialize d> 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
2 904
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.co m

"JackRazz" <Ja******@NotVa lid.com> wrote in message
news:e8******** ******@TK2MSFTN GP10.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 <NonSerialize d> attribute is used.

Public Function NextMessageNo() As Integer
<NonSerialized( )> Static pastFirstCall As Boolean = False
'This is
being Serialized!!!!! !!!!!
If Not pastFirstCall Then
mNextMessageNum ber = mNextPersistabl eMessageNo
pastFirstCall = True
End If
NextMessageNo = mNextMessageNum ber + 1
mNextMessageNum ber = NextMessageNo
End Function
<NonSerialized( )> Private mNextMessageNum ber As Integer
Using Reflector, I can see that, internally, pastFirstCall is declared as
a private
variable as:
Private $STATIC$NextMes sageNo$2008$pas tFirstCall As BooleanThe problem is
that the
<NonSerialize d> 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
Nicholas,
Thanks for the info. Did they ever fix it with the 1.1 framework?


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message
news:%2******** *******@tk2msft ngp13.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.co m
|
| "JackRazz" <Ja******@NotVa lid.com> wrote in message
| news:e8******** ******@TK2MSFTN GP10.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 <NonSerialize d> attribute is used.
| >
| > Public Function NextMessageNo() As Integer
| > <NonSerialized( )> Static pastFirstCall As Boolean = False
| > 'This is
| > being Serialized!!!!! !!!!!
| > If Not pastFirstCall Then
| > mNextMessageNum ber = mNextPersistabl eMessageNo
| > pastFirstCall = True
| > End If
| > NextMessageNo = mNextMessageNum ber + 1
| > mNextMessageNum ber = NextMessageNo
| > End Function
| > <NonSerialized( )> Private mNextMessageNum ber As Integer
| >
| >
| > Using Reflector, I can see that, internally, pastFirstCall is declared as
| > a private
| > variable as:
| > Private $STATIC$NextMes sageNo$2008$pas tFirstCall As BooleanThe problem is
| > that the
| > <NonSerialize d> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5316
by: SLEEPY LOLO | last post by:
Hi! I have static function and three threads. I would like every thread to use this function at the same time. Is it possible? I'm using VC++ under Windows2000. Thanks Hubert
5
2749
by: Marijn | last post by:
I'd like to know how compilers usually handle static variables that are declared inside a function (as opposed to static class-members). Like in: int counter(){ static int c=0; ++c; return c; }
10
28432
by: Gernot Frisch | last post by:
Problem: A.cpp: ------ static FOO* gFoo=NULL; A.h extern FOO* gFoo; gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
16
2989
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
115
7664
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical transform function. When compiling under gcc on my big-endian PowerPC (Mac OS X), declaring this array as "static" DECREASES the transform throughput by around 5%. However, declaring it as "static" on gcc/Linux/Intel INCREASES the throughput by...
10
429
by: Peter Kirk | last post by:
Hi there I have a question about static variables. There is a function which returns a pointer to a structure "AStructure". The function has a static local variable of type AStructure, and it is the address of this variable which is returned. Is this good practice? See this pseudo code: AStructure *my_function()
2
1439
by: JackRazz | last post by:
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 =...
18
3331
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done with the StaticClass.Method1 before it can use it? What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
20
6028
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static members / singletons (especially meyer type singletons) which use locally declared static variables. These variables are normally cleaned up automatically at shutdown of the application by registering with the atexit. I break point the destructor on...
5
2330
by: Lawrence Krubner | last post by:
Do any problems come up when using a static variable in a cron job? Assuming the cron job is called every 5 minutes for one year. Assume I've got an array that stores the names of which users are logged in to a particular website at the current moment. Would it cause me trouble if I made this array static? If it would cause trouble, what's the easiest way to persist the data? I could use a database, but that seems heavy handed for a 20...
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10648
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.