Monday, 14 September 2009

ASP.NET - VIEWASTEXT Attribute


If you have a control on an ASP.NET page and Visual Studio is having trouble rendering it in design view, then you can add the VIEWASTEXT attribute to the tag, and visual studio will then render this as text.

Example:
<object id=factory style="display:none"
classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
  codebase="smsx.cab#Version=6,5,439,30 VIEWASTEXT>

    <param name="template" value="MeadCo://IE7" />
</object>

Friday, 11 September 2009

Error MSB4019: The imported project "C:\Microsoft.CSharp.targets" was not found.


If you've received this error, it usually means that the targets file can not be found/identified by the current versiuon of visual studio....

I usually receive this error when attempting to import a .NET 3.5 project into a .NET 2.0 solution.

To tackle this particular scenario, you will need to open the project file (.csproj).... and if you wish to make the project .NET 2.0 ready, you will need to find the following line (specific to 3.0/3.5)...


<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

and replace with...

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

Thursday, 10 September 2009

Visual Studio Debug Problem (Debugger Detaching)


During the development of an ASP.NET application, have you ever had the debugger randomly detach itself from the IE instance?

This is usually because VS gets confused regarding which isntance of IE to detach itself to... goto into Task Manager and see if there are any iexplore processes running that do not have a window handle. If so, end them and re-try...

If not, take a look at this post regarding new features introduced with IE8 that can cause this issue...

http://weblogs.asp.net/abdullaabdelhaq/archive/2009/06/01/VS-Debug-Problem-with-IE8.aspx

Friday, 4 September 2009

C# Conditional Operator ( ?: ) and Null-Coalescing Operator ( ?? )


Often times in code you want to assign one variable to another, but only if the variable is not null. If it is null, you want to populate the target variable with another ( perhaps default ) value. The code normally may look like this using the C# Conditional Operator:

string fileName = tempFileName !=null ? tempFileName : "Untitled;
 

If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.


This can now be abbreviated as follows using the Null-Coalescing Operator:

string fileName = tempFileName ??"Untitled";

The logic is the same. If tempFileName is not null, fileName = tempFileName, else fileName = “Untitled“.


The Null-Coalescing Operator comes up a lot with nullable types, particular when converting from a nullable type to its value type:

int? count = null;
 
int amount = count ?? default(int);

Since count is null, amount will now be the default value of an integer type ( zero ).

These Conditional and Null-Coalescing Operators aren't the most self-describing operators :), but I do love programming in C#!

Thursday, 3 September 2009

C# - Difference between 'out' and 'ref'


When passing objects as parameters to functions, there may be a certain scenario where you wish to modify a parameter before a function call, modify its valur within the function, and retain its value after the function call... This example behaviour describes the actions of the 'ref' qualifier to a function argument. 'out' however, is quite similar...

ref qualifier
When passing an object to function, specifying it as ref requires the object to be initialised beforehand. You can then modify its value inside the function and continute to use the object once the function call has ended.

Example
        private void ModifyByFive(ref int number)
        {
            number += 5;
        }


out qualifier
When passing an object to function, specifying it as out does not require the object to be initialised beforehand. However, the object can then be modified for this purpose within the function, and can be used outside the function once the function has executed. This can be treated as an 'extra return value'. However, using too many out parameters might require an architectural re-think; as a class or struct might prove more benificial.

Example
        private void ModifyByFive(out int number)
        {
            number += 5;
        }


Tuesday, 1 September 2009

Registering a DLL as an MSI Custom Action


1. In Visual Studio, open your solution and right click on the setup project. Select "View" and then select "Custom Actions".

2. Select a custom action for which you which to register the DLL, this would usually be "Install".

3. Right Click on the Install directory and select "Add Custom Action".

4. You will then need to add a script which will be run as part of the MSI installation process. For this, we will use VB Script. Paste the below code into Notepad and save it as a VB Script (.vbs) file.

REGISTER DLL VB Script
Dim WshShell
Set WshShell = CreateObject("Wscript.Shell")
WshShell.run "regsvr32 /s DLLFILENAME.dll"
Set WshShell = nothing

5. Add this to your project and reference it as a custom action for your "Install" process.

6. You may wish to unregister the DLL file during the uninstall phase. Perform the previous processes which the uninstall VB Script below...

UNREGISTER DLL VB Script
Dim WshShell
Set WshShell = CreateObject("Wscript.Shell")
WshShell.run "regsvr32 /u/s DLLFILENAME.dll
Set WshShell = nothing