Solved

Global Variable not incrementing.

Posted on 2007-03-18
10
390 Views
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()
Function()
//inside function transactions take place and then the ProcessCnt variable is incremented for each successful transaction
Txs.Commit();
++Global.ProcessCnt;


class for global variables

public class Global
    {
        public static int ProcessCnt;
0
Comment
Question by:Steve7423
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 37

Accepted Solution

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

Global.ProcessCnt = 1;

Cheers,

Greg
0
 
LVL 4

Expert Comment

by:MikeGeig
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.
0
 

Author Comment

by:Steve7423
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)
           SAfunctions.Transfer_Invoice()
           this.LblProcCnt.Text = Global.ProcessCnt.ToString();


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


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.
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Assisted Solution

by:MikeGeig
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.
0
 

Author Comment

by:Steve7423
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 ?
0
 
LVL 4

Expert Comment

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

Author Comment

by:Steve7423
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???
0
 

Author Comment

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

Author Comment

by:Steve7423
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.
0
 

Author Comment

by:Steve7423
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.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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