Solved

Global Variable not incrementing.

Posted on 2007-03-18
10
368 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Performance  Html.BeginForm vs jQuery 3 51
Entity Framework 7 33
Where can I ask about my ASP.NET MVC project? 6 23
Video Player 2017 5 17
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

22 Experts available now in Live!

Get 1:1 Help Now