CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2009
    Posts
    112

    Global Variable in method parameter

    Hi, I'm writing a program where I call a method that for one of its parameters is a global variable. The issue i'm running into is that when the calculations are done the parameter's value changes, but it does not affect the global variable. I figured it would change the global variable to. Is there something I'm missing?


    Here is the Triggered event:

    Code:
            private void txtMortguageJan_TextChanged(object sender, EventArgs e)
            {
                CalculateMortguage(0,txtMortguageJan, arMortguage, lblYrMortguage, decYrMortguage);
            }
    Here is the called Method:

    Code:
            private void CalculateMortguage(int intArrayIndex, TextBox txtTextBox, 
    decimal[] decarray, Label lblLabel, decimal decYrTotal)
            {
                txtTextBox.BackColor = Color.White;//resets the background of the text box to white
                try
                {
                    //--------------------------------------------------------------------------
                    if (txtTextBox.Text == "")// checks to see if the text box is blank
                    {
                        decarray[intArrayIndex] = 0;
                    }
                    else if (txtTextBox.Text.StartsWith("."))//checks to see of text box begins with a period
                    {
                        txtTextBox.Text = "0.";//changes the textbox to "0."
                        txtTextBox.Select(txtTextBox.Text.Length, 0);
                        decarray[intArrayIndex] = 0;//sets variable to 0
                    }
                    else//valid text entry
                    {
                        decarray[intArrayIndex] = Convert.ToDecimal(txtTextBox.Text);
                    }
                    //--------------------------------------------------------------------------
                    decYrTotal = 0;// resets the govt rent variable
                    foreach (decimal d in decarray)//loops through each month and adds them up
                    {
                        decYrTotal += d;
                    }
                    lblLabel.Text = decYrTotal.ToString("0.00");//puts the year's total into the label
                    CalculateExpensesTotal();//calls method
                }
                catch
                {
                    txtTextBox.BackColor = Color.Red;//if error it changes text box color to alert user.
                }
            }
    The global variable that is not changing is decYrMortguage, and the parameter I use it in is decYearTotal. When I debug the code, after is loops through the array decYearTotal changes, but when I look at decYrMortguage it's value remains at 0.

    Thanks.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Global Variable in method parameter

    Maybe I msised it, but I don't see anywhere in your code where you are assigning a new value to your global. You're assigning your new value to decYearTotal. A variable's value only changes if a method acts upon it to change the value (like incrementation or decrementation on a numeric) or if a new assignment takes place on that variable.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  3. #3
    Join Date
    Feb 2009
    Posts
    112

    Re: Global Variable in method parameter

    Well I am passing the global variable to the called method. I want it to change the value of decYrMortguage as decYearTotal Changes without having a specific line of code that sets the value of decYrMortguage (ex: decYrMortguage = decYearTotal; ). Because I have more that one text chaged event that will call this method so I want to pass the appropriate global variable when I call the Calculate method.

    Does that make sense?
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: Global Variable in method parameter

    Well one possible workaround, and it's not pretty or elegant, would be to pass in another parameter that contains the name of the variable you want to update. Then call another method (I'll call it UpdateGlobal for this example) that takes in the name of the global to update and the new value. Use a conditional on the name to determine which global needs the update and then have that assign the new value.
    Code:
    if (Issue.Resolved)
    {
         ThreadTools.Click();
         MarkThreadResolved();
    }

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Global Variable in method parameter

    You need to pass value types in as ref if you want the original value to be updated (otherwise value types are just copies of the originals).

    Code:
    private void CalculateMortguage(int intArrayIndex, TextBox txtTextBox, 
    decimal[] decarray, Label lblLabel, ref decimal decYrTotal)
    {
      ...
    }
    
    private void txtMortguageJan_TextChanged(object sender, EventArgs e)
    {
      CalculateMortguage(0,txtMortguageJan, arMortguage, lblYrMortguage, ref decYrMortguage);
    }

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Global Variable in method parameter

    Btw, you have your terminology incorrect. In C#, there is no such thing as a "global variable", so you are actually passing in a class field, not a global variable.

  7. #7
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Global Variable in method parameter

    Furthermore, class fields are readily available to methods of that class, so you don't need to pass it. Unless it's some kind of "convenience-method" that may operate on other data as well, in other scenarios.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured