Groups > Asp .Net > Advanced ASP.NET Architecture > Re: real hard time understanding good usage of delegates




real hard time understanding good usage of delegates

real hard time understanding good usage of delegates
Mon, 24 Mar 2008 19:10:28 +000
I been trying to understand when to use delegates, and while i have gone through
several tutorials I am still having problems using it in my every day
programming.  Here I have a simple skeleton code of an app I just started
coding,  that checks for the connectivity of different services, when I look at
this code, I wonder where could I use a delegate, or when a delegate will
enhance the readability, or efficiency of my code.  If you have any ideas I will
very much appreciate it. Also if you have any suggestions in betters was to
approach this from the architectural point of view please let me know. ( my next
step is to make an class that will be call connectivity and bind it to the
controls 

 

namespace ConnectivityMonitor {
    public partial class ConnectionTests : Form {
        public ConnectionTests() {
            InitializeComponent();
        }

        private void btnRunConnectivityTests_Click(object sender, EventArgs e)
{
            runTests();
        }



        #region set color bars methodsprivate void MakeGreen(Button oButton) {
            oButton.BackColor = Color.Green;
        }

        private void MakeGray(Button oButton) {
            oButton.BackColor = Color.Gray;
        }

        private void MakeRed(Button oButton) {
            oButton.BackColor = Color.Red;
        }
        #endregion

        #region set Messages Methodsprivate void MsgNotTested(Label oLabel) {
            oLabel.Text = "Not Tested";
        }

        private void MsgTestPass(Label oLabel) {
            oLabel.Text = "Pass";
        }
        private void MsgTestFail(Label oLabel) {
            oLabel.Text = "Fail";
        }
        #endregion

        #region DisplayMessagesprivate void DisplayNotTested(Button oButton,
Label oLabel) {
            MakeGray(oButton);
            MsgNotTested(oLabel);
        }
        private void DisplayPass(Button oButton, Label oLabel) {
            MakeGreen(oButton);
            MsgTestPass(oLabel);
        }
        private void DisplayFail(Button oButton, Label oLabel) {
            MakeRed(oButton);
            MsgTestFail(oLabel);
        }
        #endregion

        #region check connectivity methodsprivate bool PassTestRemoting() {
            return true;
        }
        private bool PassTestWebServices() {
            return true;
        }
        private bool PassTestDispatcher() {
            return true;
        }
        #endregion

        private void runTests() {

            if (PassTestDispatcher() == false) {
                DisplayFail(btnGraphicBarDispatcher, lblStatusDispatcher);
            } else {
                DisplayPass(btnGraphicBarDispatcher, lblStatusDispatcher);
            }

            if (PassTestWebServices() == false) {
                DisplayFail(btnGraphicBarWebServices, lblStatusWebServices);
            } else {
                DisplayPass(btnGraphicBarWebServices, lblStatusWebServices);
            }

            if (PassTestRemoting() == false) {
                DisplayFail(btnGraphicBarRemoting, lblStatusRemoting);
            } else {
                DisplayPass(btnGraphicBarRemoting, lblStatusRemoting);
            }

        }



    }
}
Post Reply
Re: real hard time understanding good usage of delegates
Tue, 25 Mar 2008 14:28:38 +000
"JohnStern" wrote in message news:2251706@forums.asp.net...

I been trying to understand when to use delegates, and while i have gone through
several tutorials I am still having problems using it in my every day
programming.  Here I have a simple skeleton code of an app I just started
coding,  that checks for the connectivity of different services, when I look at
this code, I wonder where could I use a delegate, or when a delegate will
enhance the readability, or efficiency of my code.  If you have any ideas I will
very much appreciate it. Also if you have any suggestions in betters was to
approach this from the architectural point of view please let me know. ( my next
step is to make an class that will be call connectivity and bind it to the
controls 

 

namespace ConnectivityMonitor {
    public partial class ConnectionTests : Form {
        public ConnectionTests() {
            InitializeComponent();
        }

        private void btnRunConnectivityTests_Click(object sender, EventArgs e)
{
            runTests();
        }



        #region set color bars methodsprivate void MakeGreen(Button oButton) {
            oButton.BackColor = Color.Green;
        }

        private void MakeGray(Button oButton) {
            oButton.BackColor = Color.Gray;
        }

        private void MakeRed(Button oButton) {
            oButton.BackColor = Color.Red;
        }
        #endregion

        #region set Messages Methodsprivate void MsgNotTested(Label oLabel) {
            oLabel.Text = "Not Tested";
        }

        private void MsgTestPass(Label oLabel) {
            oLabel.Text = "Pass";
        }
        private void MsgTestFail(Label oLabel) {
            oLabel.Text = "Fail";
        }
        #endregion

        #region DisplayMessagesprivate void DisplayNotTested(Button oButton,
Label oLabel) {
            MakeGray(oButton);
            MsgNotTested(oLabel);
        }
        private void DisplayPass(Button oButton, Label oLabel) {
            MakeGreen(oButton);
            MsgTestPass(oLabel);
        }
        private void DisplayFail(Button oButton, Label oLabel) {
            MakeRed(oButton);
            MsgTestFail(oLabel);
        }
        #endregion

        #region check connectivity methodsprivate bool PassTestRemoting() {
            return true;
        }
        private bool PassTestWebServices() {
            return true;
        }
        private bool PassTestDispatcher() {
            return true;
        }
        #endregion

        private void runTests() {

            if (PassTestDispatcher() == false) {
                DisplayFail(btnGraphicBarDispatcher, lblStatusDispatcher);
            } else {
                DisplayPass(btnGraphicBarDispatcher, lblStatusDispatcher);
            }

            if (PassTestWebServices() == false) {
                DisplayFail(btnGraphicBarWebServices, lblStatusWebServices);
            } else {
                DisplayPass(btnGraphicBarWebServices, lblStatusWebServices);
            }

            if (PassTestRemoting() == false) {
                DisplayFail(btnGraphicBarRemoting, lblStatusRemoting);
            } else {
                DisplayPass(btnGraphicBarRemoting, lblStatusRemoting);
            }

        }



    }
} 
http://forums.asp.net/p/1237719/2251706.aspx#2251706

 void RunTest(Button button, Label label, Func<bool> test) {   bool pass =
test.Invoke();   button.BackColor = pass ? Color.Green : Color.Red;   label.Text
= pass ? "Pass" : "Fail";}
void RunLambdaTests() {   RunTest(btnGraphicBarDispatcher, lblStatusDispatcher,
() => true);   RunTest(btnGraphicBarWebServices, lblStatusWebServices, ()
=> true);   RunTest(btnGraphicBarRemoting, lblStatusRemoting, () =>
true);} // real work may be easier with a delegate rather than a lambdavoid
RunDelegateTests() {   RunTest(btnGraphicBarDispatcher, lblStatusDispatcher,
delegate() {      var dispatcher = new Dispatcher() {         ..RemoteEndPoint =
IPEndPoint.Parse("remote.example.com:2045"),         ..Async = false  
   };      return dispatcher.Ping();   });}
Post Reply
Re: real hard time understanding good usage of delegates
Tue, 25 Mar 2008 16:57:16 +000
hello, thank you, this is great. I appreciate you taking the time to do this
Post Reply
about | contact