Refactor Code

Latest Refactorings
  • Gravatar
    Convert String Array to Separator String

    by azamsharp on 7/24/2008 9:16:29 AM (C#)
    Refactorings => 1
  • Here is a small piece of code that converts a string array to a separated string. The separator can be a comma, space etc.
  • // create a string out of an array
    private static string GetSeparatedStringFromArray(string[] source, string separator)
    {
    StringBuilder sb = new StringBuilder();

    foreach (string str in source)
    {
    sb.Append(str);
    sb.Append(separator);
    }

    return sb.ToString().TrimEnd(separator);
    }
  • Gravatar
    Query XML using XPathDocument and XPathNodeIterator

    by vipraIT on 7/22/2008 10:49:45 AM (XML)
    Refactorings => 0
  • I try to query XML to retreive a particular element value but it gives complete XML document instead of particular element or node. XML document is like- Autobiography of Bebo Dev Robin 999.99
  • private void QueryXml()
    {
    XPathDocument document = new XPathDocument("\\books.xml");
    XPathNavigator navigator = document.CreateNavigator();

    // Select all books authored by Robin
    XPathNodeIterator nodes = navigator.Select("descendant::book [author/last-name='Robin']");
    while (nodes.MoveNext())
    {
    Clone the navigator returned by the Current property.
    // Use the cloned navigator to get the title element.
    XPathNavig
  • Gravatar
    Extract File Name from URL Using JavaScript

    by azamsharp on 7/18/2008 1:21:19 PM (JavaScript)
    Refactorings => 0
  • I found this nifty code online that extracts file name from the url using JavaScript code. This can be performed better by using Regular Expressions (any takes?)
  • function getFileNameFromUrl(url)
    {
    wholeurl = url;
    x = wholeurl.length;
    while((wholeurl.substring(x,x-1)) != "/"){ x--; }
    return wholeurl.substring(wholeurl.length,x);
    }
  • Gravatar
    Closing Popups When the Parent Window is Closed

    by azamsharp on 7/8/2008 6:50:22 PM (JavaScript)
    Refactorings => 0
  • My friend recently asked me how to close the child windows when the parent is closed. Here is the code that I came up with:
  • // create an array of popup window object. This is a global variable
    var popups = new Array();

    function openPopUp(url,name)
    {
    var popUp = window.open(url,name,"width=100,height=100");
    popups.push(popUp);
    }


    <body onunload="destroyPopUps()">

    function destroyPopUps()
    {
    if(popups.length == 0) return;

    for(i=0; i<popups.length; i++)
    {
    popups[i].close();
    }
    }
  • Gravatar
    Filling a DropDownList Using XML

    by azamsharp on 7/8/2008 6:48:34 PM (XML)
    Refactorings => 6
  • Some time back I had the requirement to fill the DropDownList control using XML file. Here is the code:
  • // The XML file
    <?xml version="1.0" encoding="utf-8" ?>

    <names>

    <name>

    <client>hank</client>

    </name>

    <name>

    <client>corry</client>

    </name>

    <name>

    <client>david</client>

    </name>

    <name>

    <client>james</client>

    </name>

    </names>

    And here is the C# code:

    XmlDocument doc = new XmlDocument();

    doc.Load(Server.MapPath