Wednesday, September 15, 2010

Access to modified closure !

My colleague asked me do you know about the warning re-sharper gives called " Access to modified closure"...
I said ... Nope.. and what he told me was fascinating !


Lets say you have a code something like this:


            List strList=new List();
            strList.Add("one");
            strList.Add("two");
            strList.Add("three");
            strList.Add("four");

            int y = 20;
            foreach (string s in strList)
            {
                Button btn =new Button();
                btn.Text = s;
                btn.Location=new Point(10,y);
                btn.Click += delegate
                                 {
                                     MessageBox.Show(s);
                                 };
                panel1.Controls.Add(btn);
                y += 40;
            }

What it does is, creates a button and adds it to a panel, the text is set as the string in the list and click event shows the string in the message.


What really happens is ... you get buttons with text as "one", "two", "three", "four" but if you click all of them, all of them show "four" !!


This is because, the modified value of the variable is never passed to the closure, which is our anonymous delegate here.


The solution is:


            List strList=new List();
            strList.Add("one");
            strList.Add("two");
            strList.Add("three");
            strList.Add("four");

            int y = 20;
            foreach (string s in strList)
            {
                string s1 = s;
                Button btn =new Button();
                btn.Text = s;
                btn.Location=new Point(10,y);
                btn.Click += delegate
                                 {
                                     MessageBox.Show(s1);
                                 };
                panel1.Controls.Add(btn);
                y += 40;
            }

Get the value in a separate variable and pass that variable each time in the delegate.

Monday, June 28, 2010

Attaching the debugger to a custom action in web setup/installer using Visual Studio

I wanted to debug the custom installer I had created when running the web setup installer in Visual Studio.


Finally after some searching through google, I found out how to do it.


First of all, in order to get it running correctly, open the VS in Administrator mode.


Then add 
Debugger.Launch() just before the line where you want to debug.


Then build and right click -> Install on the installer project.


It will get to the point where you have hit debugger.launch() and pop up a message to select the debugger instance to be used.

Thursday, June 10, 2010

How to check if internet connection is available or not in WPF

In one of the applications I wanted to know if I am connected to internet or not... I looked around and found this solution.


I guess it has some problems when VPN connections are setup on the machine. But otherwise it works just fine.


Steps:
1] Refer System.net dll in your WPF project
2] Where you want to check for the connection, use this code


 NetworkInterface.GetIsNetworkAvailable();

..

Tuesday, June 1, 2010

Update Panel and Update progress in ASP.net

I was working on UpdatePanel and UpdateProgress controls in the ASP.net Ajax extensions.


My requirement was simple : I wanted a list of icons or image buttons on the left hand side, which when clicked would display some relevant content on the right hand side update panel asynchronously.


So I had a script manager under which I have an update panel in one div tag.
And the left side panel in a separate div tag.


Something like this:


So that when I click on "button1" it will display some content in the update panel and when I click on "button2" it will fetch some different content using AJAX and display it.


It worked fine with a code behind in button click events.


However, I thought I should display the UpdateProgress once the user clicks on the buttons. 
like "Please wait..." or "Updating..." with a ".gif" image ...


So I added the UpdateProgress inside the panel in a div tag like this :





<div id="divWait" style="font-size: large; position: absolute; left: 600px; top: 400px;">
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1"
DisplayAfter="100" DynamicLayout="true">
  <ProgressTemplate>
      <div>
       <img border="0" src="./ig_res/Default/images/ig_progressIndicator.gif" />
         Please wait...
      div>
  ProgressTemplate>
 asp:UpdateProgress>
div>


Turns out this does not work !!

I read lot of forums and stuff online but could not find a solution.

Then all I did was get the button inside the update panel and it started working !!

I am yet to find a solution to the problem ... can I not have buttons outside the Update Panel and have the Update Progress control working??