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

Home Posts Topics Members FAQ

weird behavior with static int

Hi,
GetNextSequence Number2 worked for me in a different class..
but now it does not work.. it does not increment..
will it not get incremented after the return?
private static int GetNextSequence Number1()
{

lock (padlock)
{
return ++_sequenceNumb er;
}
}
private static int GetNextSequence Number2()
{

lock (padlock)
{
return _sequenceNumber ++;
}
}

private static int _sequenceNumber = 0;

static readonly object padlock = new object();
Jun 27 '08 #1
4 1199
On Thu, 24 Apr 2008 15:49:35 -0700, parez <ps*****@gmail. comwrote:
GetNextSequence Number2 worked for me in a different class..
but now it does not work.. it does not increment..
You'll need to post a concise-but-complete code sample that demonstrates
the problem.
will it not get incremented after the return?
In either version of the method you posted, the variable will be
incremented _before_ the return. In one version, it's incremented before
being evaluated as the return value, and in the other it's incremented
after. But in either case, the variable will have its new value before
control is returned to the caller.

In neither version is there an obvious problem, so whatever is going
wrong, it's in a part of the code you didn't show us. Either the code you
posted isn't actually the code that's generating the sequence numbers, or
you're mistaken about whether it works or not.

The only way for anyone to figure out what's actually going wrong is to
see a complete code example. Making it concise will ensure that someone
will bother looking at it. :)

By the way, for what it's worth, if all you're doing is incrementing a
variable, you may find the Interlocked class a better solution than using
the lock() statement. Not that there's anything wrong with using lock()
per se; just that you might prefer the alternative.

Pete
Jun 27 '08 #2
On Thu, 24 Apr 2008 15:49:35 -0700 (PDT), parez <ps*****@gmail. comwrote:
>GetNextSequenc eNumber2 worked for me in a different class..
but now it does not work.. it does not increment..
will it not get incremented after the return?
private static int GetNextSequence Number1()
{

lock (padlock)
{
return ++_sequenceNumb er;
}
}
private static int GetNextSequence Number2()
{

lock (padlock)
{
return _sequenceNumber ++;
}
}

private static int _sequenceNumber = 0;

static readonly object padlock = new object();
If sequenceNumber is currently 42 and we do this:

int value = GetNextSequence Number1();

after the call, both value and _sequenceNumber will be 43.

If we do this (sequenceNumber again being 42):

int value = GetNextSequence Number2();

after the call, value will still be 42, but _sequenceNumber will have been incremented.

That's just the way postincrement works.

Might that be the problem?

Regards,
Gilles.

Jun 27 '08 #3
On Apr 24, 7:21 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Thu, 24 Apr 2008 15:49:35 -0700, parez <psaw...@gmail. comwrote:
GetNextSequence Number2 worked for me in a different class..
but now it does not work.. it does not increment..

You'll need to post a concise-but-complete code sample that demonstrates
the problem.
will it not get incremented after the return?

In either version of the method you posted, the variable will be
incremented _before_ the return. In one version, it's incremented before
being evaluated as the return value, and in the other it's incremented
after. But in either case, the variable will have its new value before
control is returned to the caller.

In neither version is there an obvious problem, so whatever is going
wrong, it's in a part of the code you didn't show us. Either the code you
posted isn't actually the code that's generating the sequence numbers, or
you're mistaken about whether it works or not.

The only way for anyone to figure out what's actually going wrong is to
see a complete code example. Making it concise will ensure that someone
will bother looking at it. :)

By the way, for what it's worth, if all you're doing is incrementing a
variable, you may find the Interlocked class a better solution than using
the lock() statement. Not that there's anything wrong with using lock()
per se; just that you might prefer the alternative.

Pete
private static int _sequenceNumber = 0;

static readonly object padlock = new object();
#endregion

private static int GetNextSequence Number()
{

lock (padlock)
{
return _sequenceNumber ++;
}
}
I was doing something stupid..

I had a
_sequenceNumber =ClassName.GetN extSequenceNumb er();

thats _sequenceNumber ++ didnt work and ++_sequenceNumb er; did.

I refactored some of my existing code, used copy-paste in the process
and really screwed up the refactoring..

I will look into interlocked classes..

Thanks
Jun 27 '08 #4
On Apr 25, 9:49*am, parez <psaw...@gmail. comwrote:
I will look into interlocked classes..

Thanks
One word of caution...if you have another method in the class that is
reading _sequenceNumber more than once inside a lock(padLock)
statement then it's reasonable to say that you were implicitly
assuming the value would stay the same between reads. However, if you
convert the increment statement to use the Interlocked class your
assumption may fail possibly resulting in a bug that is difficult to
find.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2653
by: DaKoadMunky | last post by:
<CODE> #include <iostream> using namespace std; int Foo(int x,int y) { int result = x; result*=y; result+=y;
11
13725
by: Les Paul | last post by:
I'm trying to design an HTML page that can edit itself. In essence, it's just like a Wiki page, but my own very simple version. It's a page full of plain old HTML content, and then at the bottom, there's an "Edit" link. So the page itself looks something like this: <HTML><HEAD><TITLE>blah</TITLE></HEAD><BODY> <!-- TEXT STARTS HERE --> <H1>Hello World!</H1> <P>More stuff here...</P>
11
1958
by: ncf | last post by:
Ok, I've been tring to resolve this issue for some time now (~1 day which is way longer than normal for me) to no avail. I am reading a file into a list in memory, using a "%" delimited file format (which allows for comments on lines beginning with "#"), and some of the messages are not correctly copied. I believe the problem may be somewhere around the strcpy() call, but am not sure at all.
3
5823
by: Holger (David) Wagner | last post by:
Hi all, we're currently developing an application in the .NET environment that needs to access a DLL implemented in Fortran. There is one procedure with about 17 parameters, most of them arrays. After some trial and error experiments, we've found out that a) the parameters must be ordered by type, i.e. we cannot mix double, integers and Strings (char-Arrays) - instead, we first need to put all the float-Arrays and Int32-Arrays, and...
13
1358
by: Kenneth Baltrinic | last post by:
This isn't a problem from the standpoint that its easy to work around. However, I am very currious as to the correctness of this behavior. I am developing an MDI application. In the application I have defined a Toolbox class deriving from System.Windows.Forms.Form. I want only one instance of this class to ever exist and for it to be displayable by calling a static method of the class called Show(). To my surprise, my static method is...
1
5901
by: Jonathan Yong | last post by:
I observe a very weird behavior when dynamically create web control and bind events to it. Create a C# ASP.NET application, Put a PlaceHolder and Textbox onto the Web form, and try with the 4 code scenerio below. --------------------------------------------------------------------------- Scenerio 1
4
1327
by: Miro | last post by:
Vb2003, im still learning vb.net but I do not understand my output from this logic. If someone can help me out here. Cor Ligthert, you I believe were on the right track of what Im trying to create. -Thank you very much for leading me on to Classes. Ok here is my code. ( see below )
3
2406
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the class: (one I added all the asteriss to. // If we are at the end of input then NoSuchElement; // If there is still input left then InputMismatch private void throwFor() { skipped = false; if ((sourceClosed) &&...
1
1368
by: jwahlmann | last post by:
I'm experiencing some strange behavior when starting up python on a Debian-based PowerPC platform. Normally, I operate from this platform with a root file system on an IDE flash drive (/dev/hda1). However, I'm trying to get my system to run with root on a mechanical SATA drive (/dev/sda1). Both are installed on a PowerPC board via a PMC daughter board. When running off the flash drive, the Python interpreter loads and runs just fine....
0
9656
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
10370
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
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
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
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.