Friday, November 15, 2013

jQuery - this vs $(this)

The difference between this and $(this) is well explained in this StackOverflow answer ( link ).


In Summary,

$(this)[0] == this;

Of course, $(this) gets all the nice jQuery functions and properties, whereas this is javascript keyword.

Also, the author of the answer makes an intuitive point for the jQuery selector that "jQuery selector always returns array.

$('#MyUniqueDiv')[0]  == document.getElementById('MyUniqueDiv');



Even if there is only a single div with id="MyUniqueDiv" in the DOM, the following is still true.

$('#MyUniqueDiv')[0] == $('#MyUniqueDiv');




Wednesday, November 13, 2013

Extension Method explained by Scott Guthrie

Extension method is something that I find easy to forget from time to time. Scott Guthrie nicely explains it in his blog back in 2007.  All source code below comes from his blog.

using System;
using System.Text.RegularExpressions;
namespace SGExtensions
{
  public static class ScottGuExtensions
  {
    public static bool IsValidEmailAddress( this string s )
    {
      Regex regex = new Regex( @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$" );
      return regex.IsMatch( s );
    }
  }
}

The this keyword before the string type argument s tells the compiler that this Extension Method should be added to objects of type string .


The above  Extension Method can be applied as follows.
//namespace containing Extension Method Implementation
using SGExtensions; 
...
string email = Request.QueryString["email"];
if( email.IsValidEmailAddress() )
{
  ...
}

Another example of Extension Method....

using System;
using System.Collections;

namespace SGExtensions
{
  public static class ScottGuExtensions
  {
    public static bool In( this object o, IEnumerable c )
 {
   foreach( object i in c )
   {
     if( i.Equals( o )
  {
    return true;
  }
   }
   return false;
 }
  }
}

In the above example, the first parameter to the Extension Method "this object o" indicates that this Extension Method should be applied to all types that derive from the base System.Object base type. Basically, every object in .NET.


This "In( )" method allows to check to see whether a specific object is included within an IEnumerable sequence passed as an argument to the method. Because all .NET collections and arrays implement the IEnumerable interface, the "In( )" method could be a useful and descriptive method for checking whether any .NET object belongs to any .NET collection or array.


The following example uses the "In( )" method.

using system;
using SGExtensions;
s public class Test1
{
  void Test()s
  {
    string[] values = {"Microsoft", "Apple", "FaceBook"};
 bool isInArray = "Google".In( values ); // false
  }
}


using System;
using SGExtensions;
public partial class TestPage : System.Web.UI.Page
{
  void Page_Load()
  {
    if ( TextBox1.In( form1.Controls ) )
 {
   ...
 }
  }
}


using SGExtensions;
...
int [] values = { 0, 4, 5, 9, 42 };
int testValue = 45;
bool isInArray = testValue.In( values ); // false
bool isInArray2 = 42.In( values );       // true

Tuesday, November 12, 2013

Iterating files in a folder based on file extensions using LINQ

Using LINQ, iterating through specific files based on file extensions in a directory can be somewhat simplified.


using System.Linq;
using System.IO;
...

string fileExtensions = ".pdf, .tif, .jpg, .bmp";
string [] fileExtensionArray
    fileExtensions.split(',').Select(e => e.Trim().ToLower()).ToArray();

DirectoryInfo di = new DirectoryInfo("C:\\myDir");
FileInfo[] fileInfoArray = 
    di.GetFiles()
        .Where(f => fileExtensionArray.Contains(f.Extension.ToLower()))
        .ToArray();
...
foreach( var item in fileInfoArray )
{
    string fileFullPath = item.FullName;
    // Perform your work with the desired file here.
}



Monday, November 11, 2013

Login prompt keeps coming when developing Intranet MVC4 Website on IIS Express

I created a MVC 4 project using Visual Studio 2012 on Windows 7 Computer that is a standalone and not a member of an Active Directory domain. I selected the intranet application template for the MVC4 application in Visual Studio 2012, successfully created initial aplication with no error and adjusted project property settings as the initial ReadMe.txt file showed (i.e., Anonymous Authentication = Disabled, Windows Authentication = Enabled).

However, when running the website using IIS Express, the Login Prompt keeps coming up. I thought this nuisance was due to the fact that my computer was not a member of any domain and therefore, the browser is not treating my request as intranet traffic. When I enter my local Windows user name and password, the IIS Express lets me in and I see my MVC4 website loaded in IE properly. Still, having to enter my credential each time I press Ctrl + F5 is a nuisance to take care of for my productivity.


Workaround: 
  1. Run IE. Go to [Tools] --> [Internet Options] menu. Select [Security] tab.
  2. Choose [Trusted Sites] and click the [Sites] button.
  3. Add "http://localhost" to your trusted website. Click [close] button.
  4. When back to the [Security] tab, make sure the [Trusted sites] is still selected. 
  5. Click [Custom level...] button while under the [Trusted sites] context. 
  6. Scroll down to "User Authentication". Select "Automatic logon with current user name and password"

Through another test, I confirmed that when you develop a intranet website using VS 2012 and IIS Express on a computer that is a member of an Active Directory domain, you do not run into this issue of unnecessary login prompt. This issue only happens when you develop on a stand-alone Windows machines.