Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. bool delegate

bool delegate

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bonzaiholding
    wrote on last edited by
    #1

    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)

    C D N 3 Replies Last reply
    0
    • B bonzaiholding

      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)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B bonzaiholding

        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)

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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)

        B 1 Reply Last reply
        0
        • B bonzaiholding

          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)

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • D DaveyM69

            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)

            B Offline
            B Offline
            bonzaiholding
            wrote on last edited by
            #5

            Thanks, I just did it but there isn't some other easy way ?

            D 2 Replies Last reply
            0
            • B bonzaiholding

              Thanks, I just did it but there isn't some other easy way ?

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              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)

              1 Reply Last reply
              0
              • B bonzaiholding

                Thanks, I just did it but there isn't some other easy way ?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                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)

                B 1 Reply Last reply
                0
                • D DaveyM69

                  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)

                  B Offline
                  B Offline
                  bonzaiholding
                  wrote on last edited by
                  #8

                  Thanks Dave ;)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups