Solved

Global Variable not incrementing.

Posted on 2007-03-18
10
383 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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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