Unexpected behavior of a variable in a class - c#

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.

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).

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!

Related

why is my method not returning a value when called?

I'm trying to create a program that takes user input and passes it to three methods.
one method calculates total pay
one method determines shift selected
I want the last method to be the display method, but I've used a method that receives parameters to pass the information, a void method, and static which caused more problems than it helped.
Why is the method not getting the information?
This class has its own code file called Employee.
Class method to display results:
//Method to display results
public void Display(double total)
{
//main form instance to access controls
MainForm mainForm = new MainForm();
mainForm.nameSumBox .Text = Name;
mainForm.empNumSumBox.Text = EmployeeNumber.ToString();
mainForm.paySumBox .Text = total.ToString();
mainForm.shiftSumBox .Text = SelectedShift;
}
Call-site:
try
{
//variables
double totalPay;
//get user entry
employee.Name = nameBox.Text;
employee.EmployeeNumber = int.Parse(empNumBox.Text);
employee.HRPay = double.Parse(hrPayBox.Text);
employee.SelectedShift = employee.ShiftChoice(); // method to determine shift selection
//calculate pay total
totalPay = employee.CalcPay();
//display
employee.Display(totalPay);
Display Method should be
public void Display(MainForm mainForm, double total)
{
mainForm.nameSumBox.Text = Name;
mainForm.empNumSumBox.Text = EmployeeNumber.ToString();
mainForm.paySumBox.Text = total.ToString();
mainForm.shiftSumBox.Text = SelectedShift;
}
pass the mainform as parameter to the funcion.
When using Poul's solution, It was a success!
for the main file when calling the method:
employee.Display(this, totalPay);

Passing a variable between forms WINFORMS [duplicate]

Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers

Event Driven Programming

I've been reading this MSDN article and this question to try to understand events in .NET. Unfortunately, its not clicking for me and I'm having a lot of trouble. I'm trying to integrate this technique into my project, with little success.
Basically, I've got this class that will read numbers. Whenever it encounters a new number, I want it to fire an event called numberChanged.
So, I set up my event public event EventHandler numberChanged;. Later on, I fire my event when it encounters a number than isn't the same as the previous one.
if(currentNumber != previousNumber){
if(numberChanged != null){
numberChanged(this, new EventArgs());
}
}
But then I'm having trouble 'subscibing' to this event. If I do numberChanged += [something to do here] it errors saying that numberChanged is an event and not a type.
Is my explanation clear enough for some advice to be offered? Many thanks.
There are a number of ways to handle it, the most basic is to create a function:
public void MyNumberChangedHandler(object sender, EventArgs e)
{
//Your code goes here that gets called when the number changes
}
You then subscribe (one time only, usually in the constructor) by going:
numberChanged += MyNumberChangedHandler;
Or, you can use something called an anonymous (lambda) method, which is also assigned in your constructor (typically):
numberChanged += (sender, e) => {
//Your code here to handle the number changed event
};
To expand a little bit, care must be taken when using the lambda approach since you can create memory leaks and zombie objects. The .NET memory garbage collector is a mark-and-sweep system that removes objects when they are no longer in use. This post shows how hard it is to remove lambda event handlers: How to remove a lambda event handler .
Having an active event handler can keep your object alive even if it has been disposed! Here is an example of creating a zombie object (doesn't run in Fiddle but you can copy to your own console app) https://dotnetfiddle.net/EfNpZ5
Prints out:
I'm still alive
I'm still alive
I was disposed!
Press any key to quit
I'm still alive
I'm still alive
I'm still alive.
As everything else in the C# programming world, the events concept also follows specific rules and has it's own syntax. The wording is as follows:
an event defined as EventHandler is actually just a shortcut for a special method (delegate) signature - public delegate void EventHandler(object sender, EventArgs e)[1]. Whenever you have a signature in C# you always know what you need to write on the right sight or as a parameter, in order to connect/call some objects/methods/and so on.
after the event is defined, you need to subscribe in order to be informed whenever something happens. The syntax for subscribing an event is +=. Naturally for unsubscribing is -=. MSDN says that the syntax should be object.event += eventHandler (or object.event += new EventHandler(eventHandler);)
so after an event is defined (event Event SomeEvent;) all that left is to create a method that can be bound to this event. This method has to have the same signature as the EventHandler, so it should match the signature of [1] and can be something like private void numberChangedEventHandler(object sender, EventArgs eventArguments)
Now you know what you need to write on the right side of +=.
An example:
public class NumberSequence
{
// numbers to be compared
private readonly List<int> numbers = new List<int>();
// used to generate a random collection
private readonly Random random = new Random();
// tell me if the previous and next number are different
public event EventHandler DifferentNumbersEvent;
public NumberSequence()
{
// fill the list with random numbers
Enumerable.Range(1, 100).ToList().ForEach(number =>
{
numbers.Add(random.Next(1, 100));
});
}
public List<int> Numbers { get { return numbers; } }
public void TraverseList()
{
for (var i = 1; i < this.numbers.Count; i++)
{
if (this.numbers[i - 1] != this.numbers[i])
{
if (this.DifferentNumbersEvent != null)
{
// whoever listens - inform him
this.DifferentNumbersEvent(this, EventArgs.Empty);
}
}
}
}
}
Now before the class is used, define the event handler, that will listen and will be called, when the event is fired (wording again):
private void differentNumberEventHandler(Object sender, EventArgs eventArguments)
{
Console.WriteLine("Different numbers...");
}
And the usage:
var ns = new NumberSequence();
ns.DifferentNumbersEvent += differentNumberEventHandler;
ns.TraverseList();
Everything else is just syntactic sugar for this notation (lambda / anonymous methods / ...), for example:
object.Event += (s, e) => { // code ... }; is the same as object.Event += (Object sender, EventArgs eventArguments) => { // code ... };. Do you recognise the signature? - it is the same as the private void differentNumberEventHandler....
Often we need to pass information through the event, in this case maybe we want to see the two numbers. C# allows you to do this easily using custom event arguments. Just create a class that inherits the EventArgs class and add properties for the data that should be passed, in this case the numbers:
public class NumbersInfoEventArgs : EventArgs
{
public int Number1 { get; set; }
public int Number2 { get; set; }
}
And then specify, when declaring the event, that it will pass data of type NumbersInfoEventArgs (signatures again):
public event EventHandler<NumbersInfoEventArgs> DifferentNumbersEvent;
...
this.DifferentNumbersEvent(this, new NumbersInfoEventArgs
{
Number1 = this.numbers[i - 1],
Number2 = this.numbers[i]
});
And last but now least, the signature of the event handler should match the signature of the event:
private void differentNumberEventHandler(Object sender, NumbersInfoEventArgs eventArguments)
{
Console.WriteLine("Different numbers {0} - {1}", eventArguments.Number1, eventArguments.Number2);
}
And voila, the output is:
Different numbers 89 - 86
Different numbers 86 - 53
Different numbers 53 - 12
Different numbers 12 - 69
you can subscribe the event in this way:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var num = new Number();
num.numberChanged +=(s,e) =>{
Console.WriteLine("Value was changed to {0}",num.Value); // in the demo below you can find another implementation for this sample using custom events
};
num.Value=10;
num.Value=100;
}
}
public class Number{
public event EventHandler numberChanged;
private int _value=0;
public int Value
{
get{
return _value;
}
set{
if(value!=_value){
_value=value;
if(numberChanged!=null)
numberChanged(this,null);
}
}
}
}
explanation:
since the EventHandler delegate has 2 parameters (sender, eventArgs) as mentioned here, you need to pass these params and I passed them as s and e
another way to subscribe this event like this:
var num = new Number();
num.numberChanged += NumberChanged_Event; // below is the delegate method
public void NumberChanged_Event(object sender,EventArgs e)
{
// your code goes here
}
I updated the demo to work with you own delegate to pass the old value and new value which can help in many cases.
here a working demo

Addition in Button Event C#

private void button2_Click(object sender, EventArgs e)
{
int a,sum=0;
a = 10;
sum = sum + a;
MessageBox.Show( sum + "Sum Result");
}
Every time I click on the button I get the answer 10. I want to store result. Suppose I click 5 times then there should be 50.The above code for better understanding should give you some idea of what I'm going for.
Other option if possible I get this result outside of the button event by some method. I am new in C# so feeling lot of problem.
As #tnw tried to tell you - move sum outside the function like this:
private sum = 0;
private void button2_Click(object sender, EventArgs e)
{
sum = sum + 10;
MessageBox.Show( sum + "Sum Result");
}
This should work
explanation
The problem you face is that every variable declared inside a function will get initialized every time you call this function and is discarded when you exit the function.
With the variable beeing outside you made it a field of your class and it will be kept in memory as long as the instance you are using (for example the instance of your form) will be.
It is because sum=0; gets re-initialized every time you call the function. Try setting it as a global variable inside the Class and then call it.
This way, when ever the function will be called, the value of sum won't get back to 0, but will increment from where it was left.
// somewhere above
int sum = 0;
// then the function
private void button2_Click(object sender, EventArgs e)
{
sum = sum + 10;
MessageBox.Show(sum + " = Sum Result");
}
..since you're not using a or b. I have removed them. However, if you're using them inside the function (in a code which you haven't posted) please add them back.
Each time you called the function, you created new variable called sum and if gets deleted at the end of the function. So, each time you pressed the button, sum had a value of 0 in it as initially. Adding 10 to it, would always return 10.
Global variables are declared inside the class itself. Every variable inside the function (such as this one) would be recreated each time you call the function and will have the value you're providing it with. So it is a better approach to write the variables globally, whose value is required next time.

At what point do the Input Parameters of a WCF Workflow Service get instantiated/set?

I have a very simple XOML file with a single Code Activity inside the ReceiveActivity Handler.
The ReceiveActivity is mapped to an Interface called IRulesEngineService wih a single method on it.
void DoWork(int i);
the input parameter on the interface method is mapped to a property on the Xoml called I
I'm now trying to attempt to step into the Workflow Life Cycle at a point:
just before the first (and in this case only) Code Activity gets executed
just after the i parameter has been assigned to I on my Workflow.
I've tried overriding all the various methods and events on the XOML but in all cases I is always zero in all the events and overrides I've tried. And then is correctly set to the passed in Parameter within the first Code Activity. e.g. Imagine I passed in 8 to the ClientSide DoWork call.
public int I {get; set;}
protected override void Initialize(System.IServiceProvider provider)
{
I = I*10; //I is still 0
base.Initialize(provider);
I = I * 10; //I is still 0
}
//Event on Xoml Designer
private void Pinnacle_Initialized(object sender, EventArgs e)
{
I = I * 10; //I is still 0
}
//Event on Xoml Designer
private void receiveActivity1_OperationValidation(object sender, OperationValidationEventArgs e)
{
I = I * 10; //I is still 0
}
protected override void OnActivityExecutionContextLoad(IServiceProvider provider)
{
I = I * 10; //I is still 0
base.OnActivityExecutionContextLoad(provider);
I = I * 10; //I is still 0
}
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
DataAccess.WriteToDummyData(ConnectionString, "Pinnacle From Code Activity " + I);
//I is now magically '8' what the heck set this?
}
Anyone got any ideas on where in the Workflow Lifecycle the instantiating/binding/setting of these parameters occurs.
Looking at the .NET framework source code, it's not possible to access your data inputs within the code activity before the execution. in the ReceiveActivity class in the System.Workflow.Activities namespace there's a exact point where the inputs are populated. I've taken the class source from this link
See the image below:
As you can see in the picture inputs are populated at this point:
this.OperationHelper.PopulateInputs(this, requestContext.Inputs);
The instruction above is called just before the ReceiveActivity class executes the activity. At this stage I don't think there's a public event that can be subscribed to manipulate your data before your codeActivity1_ExecuteCode gets executed.
Hope it helps.

Categories

Resources