?
Solved

Global Variable not incrementing.

Posted on 2007-03-18
10
Medium Priority
?
394 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 300 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
Independent Software Vendors: 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 200 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

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!

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

765 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