January 2008 - Posts

If you use web sites instead of web application projects and load an user control dynamically, it can be hard to set the properties from code behind since you can´t cast the instance.

What to do? WHAAAAT TO DOOOO??? Well, first of all, take a beer and chill. After that you can take a look at reflection, and especially PropertyInfo.SetValue(...).

Magic: 

using System.Reflection;

void DoMagic()
{

UserControl meLiek = LoadControl("tehcontrolz.ascx");
Type meLiekTehTajp = meLiek.GetType();
PropertyInfo prop = meLiekTehTajp.GetProperty(
"waevvah");
prop.SetValue(meLiek,
"lolz0rz", null);
ControlHolder.Controls.Add(meLiek);

}

More info:
http://msdn2.microsoft.com/en-us/library/aa330197(VS.71).aspx

Take a luk at teh LOLCat Bible! Me liek teh awsum bibel!

http://www.lolcatbible.com

You can get a free e-book from MS Press here:

http://csna01.libredigital.com/?urvs5cn3s8

Microsoft released some awesome posters for a while ago:

Microsoft .NET Framework 3.5 - Commonly Used Types and Namespaces

2007 Office System Document: Developer Posters

Windows Server 2008 Component Posters

A not so common, but very usefull operator is the double question mark operator (??). This can be very usefull while working with nullable types.

Lets say you have two nullable int:

int? numOne = null;
int? numTwo = 23;

Scenario: If numOne has a value, you want it, if not you want the value from numTwo, and if both are null you want the number ten (10).

Old solution:

if (numOne != null)
    return numOne;
if (numTwo != null)
    return numTwo;

return 10;

 Or another solution with a single question mark:

return (numOne != null ? numOne : (numTwo != null ? numTwo : 10));

But with the double question mark operator we can do this:

return ((numOne ?? numTwo) ?? 10);

As you can see, the double question mark operator returns the first value that is not null.

If you haven´t noticed, Visual Studio Team System 2008 Team Foundation Server Power Tools have been released.

For more information look here:
http://blogs.msdn.com/bharry/archive/2007/12/21/december-07-release-of-the-tfs-power-tools-for-tfs-2008-are-available.aspx

Download:
http://msdn2.microsoft.com/en-us/tfs2008/bb980963.aspx

More Posts