|
-
April 25th, 2011, 10:09 AM
#1
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
-
April 25th, 2011, 11:03 AM
#2
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();
}
-
April 25th, 2011, 11:47 AM
#3
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
-
April 25th, 2011, 12:15 PM
#4
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();
}
-
April 25th, 2011, 01:10 PM
#5
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);
}
-
April 25th, 2011, 01:13 PM
#6
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.
-
April 25th, 2011, 02:42 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|