Tuesday, August 14, 2007

I see this a lot:

public int CompanyID

{

    get

    {

        if (ViewState["CompanyID"] == null)

        {

            ViewState["CompanyID "] = 0;

        }

        return int.Parse(ViewState["CompanyID "].ToString());

    }

    set { ViewState["CompanyID "] = value; }

}


What is wrong with it? 2 things:

1.       ViewState stores and returns an object so the get{} portion of the property that first converts this to a string and then parses it back to an integer is rather inefficient.

2.       This particular property is wrapping a value type (int) and as such, there is no need to persist the default value in  ViewState. This unnecessarily adds to the size of ViewState on a page. A better approach would be to return the default value (or any value you choose) when the ViewState object is null.

So, a better solution:

public int CompanyID

{

    get { return (int)(ViewState["CompanyID"] ?? 0); }

    set { ViewState["CompanyID"] = value; }

}
 

Remember that the C# coalescing operator (??) returns the left side value if said value is non-null and the right side value when the left side is null. We don't create an entry in ViewState until we assign a non-nulll value.

One thing to watch out for when using this pattern is when storing certain reference types in the ViewState object.

private Dictionary<Guid, bool> ToAttach

{

    get

    {

        if (ViewState["ToAttach"] == null)

        {

            ViewState["ToAttach "] = new Dictionary<Guid, bool>();

        }

        return (Dictionary<Guid, bool>)ViewState["ToAttach "];

    }

    set { ViewState["ToAttach "] = value; }

}

Testing for null and assigning an empty dictionary in the get{} portion of the property insures that we always return the same object once it is newed up. If we didn’t perform this test and tried using the coalescing operator as we did with the value type above, we would return a different object each time the property is reference and any code attempting to manipulate the object without explicitly setting the value would fail.

kick it on DotNetKicks.com   Tuesday, August 14, 2007 10:08:49 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, June 26, 2007

The server hosting my exchange account had a major hardware failure last week that eventually resulted in a full rebuild and restore. I was without email for 3 days. No major complaints and in the end, I didn't lose anything.

 

The only issue: somehow my Outlook / Exchange based RSS feeds failed to re-sync after the restore. These were already very flakey and I was already giving serious thought to moving back to News Gator. The server failure and subsequent syncing issues only pushed me over the edge. I really tried to make the Microsoft solution work but in the end, I just couldn't handle it any more.

 

kick it on DotNetKicks.com   Tuesday, June 26, 2007 1:44:21 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Wednesday, June 20, 2007

I have been using this little piece of code to recursively search my control collections for a given control or type of controls and thought that it might be of some use to others. The general purpose utility code is here:

public static void SearchControls(ControlCollection controls, Action<Control> action)

{

    foreach (Control control in controls)

    {

        if (control.HasControls())

        {

            SearchControls(control.Controls, action);

        }

        action(control);

    }

}

 

For example, if you want to search for and Trim() all TextBox controls within a GridView, you could call the routine as follows using an anonymous method:

 

SearchControls(GridViewJobs.Controls, delegate(Control control)

{

    TextBox textBox = control as TextBox;

    if (textBox != null)

    {

        textBox.Text = textBox.Text.Trim();

    }

});

 

If you need to search a GridView footer for a drop down list, you call the code as follows:

 

ControlUtility.SearchControls(gvwOrganizations.FooterRow.Controls, delegate(Control control)

{

    DropDownList dropDownListCategory = control as DropDownList;

    if (dropDownListCategory != null)

    {

        categoryID = int.Parse(dropDownListCategory.SelectedValue);

    }

});

 

 

kick it on DotNetKicks.com   Wednesday, June 20, 2007 8:01:19 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, May 22, 2007

As much as I like to think that Comcast is at the center of the evil empire, they do on occasion come through.

Today I went into my local Comcast office to upgrade my service now that I have an HD TV. As expected, there is no change to my monthly bill to get HD service. They did ding me for $10 or $12 / month to get the Discovery Channel in HD which I felt was kind of a marketing gimmick. Discovery is the premier HD channel and not including it in their basic package makes for an easy up sell.

30 seconds later, their service person handed me my new cable box with a set of component video cables. I quickly remembered that I needed a set of component cables for my DVD which previously connected to my old TV using s-video. I asked if I could get a second set of cables? Sure. Then I noticed that the cable box had a DVI connector on it but no HDMI. Could I get a DVI to HDMI converter cable? (I konw DVI and HDMI are pin compatable but that cable would have cost me $30 at one of the local big box stores.) Sure here is your HDMI / DVI cable. She apologized for not having an audio cable that I could have. I walked out of there with at least $50 worth of cables. The experience was entirely about making sure that I had everything I needed to make my TV work. I felt pretty good about the whole thing.

 

kick it on DotNetKicks.com   Tuesday, May 22, 2007 1:10:35 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, May 08, 2007

Over the weekend, Moby and Aaron both hit big milestones:

 

Moby is now 15 years old. Doing great. He still gets himself up on our bed every morning. Nemo is also doing well and is 14.)

 

Aaron is now 9 months old and crawling and has also figured out how to get himself into our bed every morning but requires a bit of help.

kick it on DotNetKicks.com   Tuesday, May 08, 2007 7:21:21 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Saturday, April 28, 2007

I came across this (in an asp.net page) last week:

protected void Page_Load(object sender, EventArgs e) {

    System.Windows.Forms.MessageBoxButtons buttons = System.Windows.Forms.MessageBoxButtons.YesNo;

    System.Windows.Forms.DialogResult result;

 

    result = System.Windows.Forms.MessageBox.Show("Test Message", "Test Caption", buttons);

}

 

kick it on DotNetKicks.com   Saturday, April 28, 2007 5:32:48 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [2]  | 
 Friday, April 27, 2007

I can't tell you how many times I have struggled with where to store a data item in a Check Box control. I typically need to do this when dynamically generating a series of check boxes on a page that correspond to a data item. I have used the tool tip which works ok but exposes your data to the use when the hover over the control. I have seen others manipulate part of the control ID to contain a data item but this seems problematic. Then it came to me:

using System;

 

namespace Utility {

 

    public class CheckBox: System.Web.UI.WebControls.CheckBox {

 

        public CheckBox() { }

 

        public object Data {

            get { return ViewState["Data"]; }

            set { ViewState["Data"] = value; }

        }

    }

}

Download a sample solution.

kick it on DotNetKicks.com   Friday, April 27, 2007 8:59:45 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

I am guessing that like most other blogs, people come across my piece of cyber space through a search engine. Just validating this theory.

Also thinking that I will try to post more frequently now that I have a really unique name!

 

-Andy

kick it on DotNetKicks.com   Friday, April 27, 2007 1:07:46 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [2]  | 
 Monday, February 05, 2007
http://www.netgear.com/Products/CommunicationsVoIP/Skype/SPH200D.aspx

I bought a Netgear / Skype phone and just love it. The real test will be if Jennifer is willing to use it over the long term. She tried it a couple of times over the weekend and I am guessing that it will all work out. $15 for the Skype plan and $150 or so for the phone.

I looked at a few of the Linksys phones but it just seemed silly to me that I needed to be logged on to my computer and running Skype to use the phone. That requirement was only slightly better than just using my headset and microphone. The Linksys 802.11 phone was an option but I didn't want a cell phone sized phone. That would have certainly caused it to fail the Jennifer test.

I also like that the Netgear phone uses standard AAA NiMh batteries. The sounds quality seems good. No noticlable echo or delay, only a bit of background static.

-A

kick it on DotNetKicks.com   Monday, February 05, 2007 1:35:15 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |