bool delegate
-
Hello , If i have event that build on this kind of delegate : bool function(int num) and i want the event to check more then one condition and will return if every thing is legal how can i do it ? (it's allways return the last result)
-
Hello , If i have event that build on this kind of delegate : bool function(int num) and i want the event to check more then one condition and will return if every thing is legal how can i do it ? (it's allways return the last result)
return a struct that contains many bools, or pass some in as out parameters.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hello , If i have event that build on this kind of delegate : bool function(int num) and i want the event to check more then one condition and will return if every thing is legal how can i do it ? (it's allways return the last result)
Where you're calling the delegate, instead of calling the delegate directly, iterate over the delegate's invocation list which you can get by calling it's GetInvocationList() method which will give you an array of instances your delegate, one for each call to be made. Check the result of each during the iteration and break out on the first false.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hello , If i have event that build on this kind of delegate : bool function(int num) and i want the event to check more then one condition and will return if every thing is legal how can i do it ? (it's allways return the last result)
bonzaiholding wrote:
and i want the event to check more then one condition and will return if every thing is legal how can i do it ? (it's allways return the last result)
Hard to tell without seeing the code.
Navaneeth How to use google | Ask smart questions
-
Where you're calling the delegate, instead of calling the delegate directly, iterate over the delegate's invocation list which you can get by calling it's GetInvocationList() method which will give you an array of instances your delegate, one for each call to be made. Check the result of each during the iteration and break out on the first false.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Thanks, I just did it but there isn't some other easy way ?
-
Thanks, I just did it but there isn't some other easy way ?
Not really. This is the way delegates work - each delegate in the invocation list is called in the order they were added. If the delegate is altering or returning a value, the last one in the list could potentially override all the previous ones. A good example of this is the FormClosingEventArgs. It's derived from CancelEventArgs which has the boolean Cancel property. If you have say two subscribers and the first one sets Cancel to true to stop the form closing, the second one can set it to false and the form will still close!
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Thanks, I just did it but there isn't some other easy way ?
You could pass a parameter and check that in each handler:
using System;
using System.Windows.Forms;public partial class Form1 : Form
{
TestClass testClass;public Form1() { InitializeComponent(); testClass = new TestClass(); testClass.TestEvent += new EventHandler<TestEventArgs>(testClass\_TestEvent); testClass.TestEvent += new EventHandler<TestEventArgs>(testClass\_TestEvent); Shown += new EventHandler(Form1\_Shown); } void Form1\_Shown(object sender, EventArgs e) { testClass.PerformTest(); } void testClass\_TestEvent(object sender, TestEventArgs e) { if (e.TestBool != true) { e.TestBool = true; MessageBox.Show("Changed to true"); } else { MessageBox.Show("Already true"); } }
}
public class TestClass
{
public event EventHandler<TestEventArgs> TestEvent;protected virtual void OnTestEvent(TestEventArgs e) { EventHandler<TestEventArgs> eh = TestEvent; if (eh != null) eh(this, e); } public void PerformTest() { OnTestEvent(new TestEventArgs()); }
}
public class TestEventArgs : EventArgs
{
public bool TestBool
{
get;
set;
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
You could pass a parameter and check that in each handler:
using System;
using System.Windows.Forms;public partial class Form1 : Form
{
TestClass testClass;public Form1() { InitializeComponent(); testClass = new TestClass(); testClass.TestEvent += new EventHandler<TestEventArgs>(testClass\_TestEvent); testClass.TestEvent += new EventHandler<TestEventArgs>(testClass\_TestEvent); Shown += new EventHandler(Form1\_Shown); } void Form1\_Shown(object sender, EventArgs e) { testClass.PerformTest(); } void testClass\_TestEvent(object sender, TestEventArgs e) { if (e.TestBool != true) { e.TestBool = true; MessageBox.Show("Changed to true"); } else { MessageBox.Show("Already true"); } }
}
public class TestClass
{
public event EventHandler<TestEventArgs> TestEvent;protected virtual void OnTestEvent(TestEventArgs e) { EventHandler<TestEventArgs> eh = TestEvent; if (eh != null) eh(this, e); } public void PerformTest() { OnTestEvent(new TestEventArgs()); }
}
public class TestEventArgs : EventArgs
{
public bool TestBool
{
get;
set;
}
}Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)Thanks Dave ;)