Reputation: 4199
I have a system timer firing an event every 10 seconds. So every 10 seconds I call the class "Termocoppia" from the main thread of the form transferring the value of "milliV" to it and expecting to get back the value of variable "tempEx".
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
timer.Tick += OnTimerTick;
timer.Interval = 10000;
timer.Start();
}
double tempEx;
//here a call the method "Calcola" in the class "Termocoppia"
private void OnTimerTick(object sender, EventArgs e)
{
double milliV = Convert.ToDouble(textBox8.Text); //I have a value of 1.111
Termocoppia thm = new Termocoppia();
thm.Calcola(milliV, tempEx);
textBox7.Text = tempEx.ToString();
}
the value milliV is then transferred to the method "Calcola" inside the class "Termocoppia". I debugged it with a break point and I confirm that the value is received in the class.
The class "Termocoppia" is like this:
public class Termocoppia
{
public double Calcola(double milliV, double tempEx)//here the value of milliV is still 1.111
{
tempEx= milliV;//here the value of tempEx is 0???
return tempEx;
}
}
I expect to receive back exactly the same value sent to the class that is well received but I keep getting back 0. If I debug the variable tempEx at the line "tempEx=milliV" the value of tempEx is 0 and I do not understand why? I am quite sure I am doing a beginner mistake here but I cannot come right with this problem.
Upvotes: 0
Views: 89
Reputation: 12944
You have two variables called 'tempEx', a field and a parameter. Your Calcola
function modifies the tempEx
parameter (not the field) and returns the same value. But the caller is not doing anything with the returned value. My suggestion is to two this value into the field tempEx
.
Modify your line:
thm.Calcola(milliV, tempEx);
into:
tempEx = thm.Calcola(milliV, tempEx);
A suggestion: Use a coding standard to prevent this kind of mistakes. For parameters use camelCasing (so tempEx), for fields use an underscore (_tempEx).
Upvotes: 2
Reputation: 958
You are not using the return value from Termocoppia.Calcola.
private void OnTimerTick(object sender, EventArgs e)
{
double milliV = Convert.ToDouble(textBox8.Text); //I have a value of 1.111
Termocoppia thm = new Termocoppia();
// the return value from Cacola has to be assigned to tempEx
tempEx = thm.Calcola(milliV, tempEx);
textBox7.Text = tempEx.ToString();
}
You should not use the same variable names for tempEx as member variable and method parameter!
Upvotes: 1