473,378 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

weird behavior with static int

Hi,
GetNextSequenceNumber2 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 GetNextSequenceNumber1()
{

lock (padlock)
{
return ++_sequenceNumber;
}
}
private static int GetNextSequenceNumber2()
{

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

private static int _sequenceNumber = 0;

static readonly object padlock = new object();
Jun 27 '08 #1
4 1177
On Thu, 24 Apr 2008 15:49:35 -0700, parez <ps*****@gmail.comwrote:
GetNextSequenceNumber2 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:
>GetNextSequenceNumber2 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 GetNextSequenceNumber1()
{

lock (padlock)
{
return ++_sequenceNumber;
}
}
private static int GetNextSequenceNumber2()
{

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 = GetNextSequenceNumber1();

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

If we do this (sequenceNumber again being 42):

int value = GetNextSequenceNumber2();

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...@nnowslpianmk.com>
wrote:
On Thu, 24 Apr 2008 15:49:35 -0700, parez <psaw...@gmail.comwrote:
GetNextSequenceNumber2 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 GetNextSequenceNumber()
{

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

I had a
_sequenceNumber=ClassName.GetNextSequenceNumber();

thats _sequenceNumber++ didnt work and ++_sequenceNumber; 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
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
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,...
11
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...
3
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....
13
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...
1
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...
4
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...
3
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...
1
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)....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.