Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Last Post for Tonight…. maybe

As I was doing a little development tonight I realized that I was using one of the new features of VS2010 and totally forgot it was new.  Some multi-line editing features make what used to be a pain a now simple process:

Optional & Named Parameters in C# 4.0

I have to say this is one of my favorite features added to this new version.  I don’t know how many times I have gone into method overload madness because C# has never supported optional parameters.  VB.Net always has…why not C#?  Well, now it does!

public void AddUser(string firstName, string lastName, string status = "New User") {
  User = new User(firstName, lastName, status);
}


Now this method could be called in a few ways:



AddUser("Sean", "Jaeger", "Working right now");
AddUser("Sean", "Jaeger");
AddUser(lastName: "Jaeger", firstName: "Sean");


Pretty darn  cool!

Tuple Type Added to C# 4.0

With C# 4.0 you can now use the new Tuple type instead of creating your very own… far simpler than using a struct or class to accomplish the same task.

There are two ways to create a Tuple in c# 4.0:

var newTuple = new Tuple<string, string, int>("Sean", "Jaeger", 36);
var newTuple = Tuple.Create<string, string, int>("Sean", "Jaeger", 36);


Type inference can also be used (but doesn’t self document):



var newTuple = new Tuple.Create("Sean", "Jaeger", 36);


You can add up to eight parameters now:



var newTuple = new Tuple<int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8);


One huge thing I see in using a Tuple rather than an anonymous type is the fact that scope can be at any level.  As we all know the scope of an anonymous type is limited to the method boundaries.



Fun stuff!

HtmlEncode() Without The HtmlEncode()

<script runat="server" language="c#">
  protected string BadHtml = "<script language='javascript'>alert('Hello');</script>";
</script>
<%=BadHtml%>


The code above would cause an alert box to pop saying “Hello” because it is not HTML encoded.  Typically what you might do here is use the Server.HtmlEncode() method within a property and use that instead.  But now with C# 4.0 & Visual Studio 2010 you can just write this:



<script runat="server" language="c#">
    protected string BadHtml = "<script language='javascript'>alert('Hello');</script>";
</script>
<%:BadHtml%>


Notice the “:” instead of “=”.  This will automatically do the encode for you.  Saving keystrokes is great!

Style Issues with IE8

As with most web developers I am sure we get frustrated when Microsoft releases a new browser. When IE7 was released it was a style disaster until they released some updates that fixed the issue... now we are at it again with the release of IE8 earlier this year.

One of the most frustrating things that I have encountered while developing for IE8 is its inability to recognize certain CSS attributes. DIV's, SPAN's, and just about anything that you position absolute on the screen gets all tweaked. I did some searching and found a fix to IE8 Compatibility Viewit... force IE8 to run in IE7 compatibility mode instead of making the user click the compatibility icon next to the address bar. 

Add this line to the HEAD section in your HTML:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />