Global Variable not incrementing.

Posted on 2007-03-18
Last Modified: 2008-02-01
For some reason a global variable is not incrementing.  I've recently moved over to C# and I'm still getting used to the syntax so I suspect this may have something to do with the problem.  why won't the variable increment.  I've debugged through it and I've watched the cursor hit ++Global.ProcessCnt; but it won't increment.

private void BtnReverse_Click(object sender, EventArgs e)

Global.ProcessCnt = 1;
Global.NotProcessCnt = 0;

//calls function()
//inside function transactions take place and then the ProcessCnt variable is incremented for each successful transaction

class for global variables

public class Global
        public static int ProcessCnt;
Question by:Steve7423
  • 6
  • 3
LVL 37

Accepted Solution

gregoryyoung earned 75 total points
ID: 18746357
you reset it to 1 eveytime you call the fuction.

Global.ProcessCnt = 1;



Expert Comment

ID: 18747921
I don't think that is inside the function call. Though it is hard to tell since no brackets were included. Would it be possible to list more of your code? I am specifically looking for the scope of the variables so brackets are a must. Greg could very well be correct, but a better listing would help me be sure.

Author Comment

ID: 18748600
Greg, the Global.ProcessCnt = 1; is called only once because it's inside the button click.  this initializes the variable.  then the function is called and the incrementing is done inside the SAfunctions.Transfer_Invoice() function.  BtnReverse has a foreach loop that loops thorugh a set of records.  each loop calles the function where the variable is incremented.

private void BtnReverse_Click(object sender, EventArgs e)
     a bunch of other stuff
     foreach (DataRow Invrow in OmnisFunctions.DSView_Invoices(startTm, endTm)
           this.LblProcCnt.Text = Global.ProcessCnt.ToString();

//Inside the SAfunctions.Transfer_Invoice()
//a bunch of transactions take place and upon successful committ increment the variable

the way it should work is that the variable is initialized to 0, (I set it to 1 just to see if it made a difference)
in the btn event.  then each time the SAfunctions.Transfer_Invoice() is called the variable should be incremented after each commit.  Then upon completion of the for each loop the variable is displayed in a lable to show the count of successful transactions.  If there are 75 successful transactions in the foreach loop then 75 should be displayed,  this.LblProcCnt.Text = Global.ProcessCnt.ToString();
 I hope that helps.

Assisted Solution

MikeGeig earned 50 total points
ID: 18748750
Steve, Can you try Global.ProcessCnt += 1; Also, if you do a managed debug and step through the process, does ProcessCnt ever get incremented at all? It is possible that something is undoing the increment, or that line of code is never being executed.

Author Comment

ID: 18748784
I stepped through and watched the line get executed and as you said it does not get incremented, it's like the incrementing is not working.

where should I place the Global.ProcessCnt += 1 ?
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Expert Comment

ID: 18748972
Instead of using ++Global.ProcessCnt, use Global.ProcessCnt += 1.  

Author Comment

ID: 18757102
interesting, I created a local variable called cnt.  right below  this.LblProcCnt.Text = Global.ProcessCnt.ToString() I placed cnt += 1. I debugged a couple loops and sure enough it's incrementing.  So what is wrong with the global variable?  a local variable will increment but not a global variable.  Obviousely there's something wrong with how I've created the global variable???

Author Comment

ID: 18757423
hmmm, ok I'm officially stumped.  the variable will not increment past 1.  

Author Comment

ID: 18757641
ooooooopppss..........  one of those cerebral farts.
it appears I initialized Global.Processcnt = 0 at the top of  SAfunctions.Transfer_Invoice() and completely forgot.  At the top of every function I declare and set variables or objects.  I must have been stoned...:) grin.

it now works.

Author Comment

ID: 18757704
I'm going to split the points because in the end you both helped.  if you have issue with it please let me know.

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using MS Code on my Mac 6 47
XML to SQL Table using c# 5 47
Trouble with References... 5 25
index Out OF Range Exception error 4 33
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now