Generating XML Comments with GhostDoc

Date Added: July 20, 2010 08:26 by By Edward
Categories: ASP.NET, Development Resources, Other

GhostDoc is a free Visual Studio extension which I stumbled accross while looking for something to help me with commenting my code. GhostDoc automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information. It takes the 'pain' out of commenting each method word for word - saving you time doing what you do best - coding!

Most documentation created will be a waste of time for a developer, and usually when there is no time to waste a developer might find the documentation is slowing him down. For any decent documentation to be useful, a developer must know that when the documentation was initially generated it was both correct and complete, it has also been updated as the project scope changed, or amendments was made to the code. I ofter find code to out of sync with documentation, which leaves me with lots of 'catch up' to do, before I feel in control and productive.

You can set it up, so when you right click on a method the "Document This" option becomes available that allows you to generate summary comments for your method.

GhostDoc comments

Here is an example of summary comments I created using this tool.       

        /// <summary>
        /// Checks for SQL injection.
        /// </summary>
        /// <param name="userInput">The user input.</param>
        /// <returns></returns>
        public static string checkForSQLInjection(string userInput)
        {
        // code here
       }

It is also supported in the following versions of the Visual Studio IDEs:

  • Visual Studio 2010
  • Visual Studio 2008
  • Visual Studio 2005

Supported Languages:

  • VB.NET
  • C#

Download it from here:

http://visualstudiogallery.msdn.microsoft.com/en-us/46A20578-F0D5-4B1E-B55D-F001A6345748

Implement a simple captcha in .Net

Date Added: May 05, 2010 18:26 by By Edward
Categories: ASP.NET, Other

I have been having several spam messages posted to my blog entries over the last 3-4 months, which now forced me to implement a captcha for when someone comments on my blog entries. I have not made this live yet, as I am also changing something something else on my blog. A captcha is a type of "challenge-response validation" test used in computing to ensure that the response is not generated by a computer. It is mostly used to protect users from unwanted spam or falsely made comments, or for people posting url's to link to other sites, for SEO benefits.

I thought I would share a few nice and easy captcha's that can be implemented by any .Net programmer.

  1. http://subkismet.codeplex.com (A stand-alone comment spam filtering library - Community project)
  2. http://recaptcha.net/plugins/aspnet/ (user control, ready to be implemented and configurable)
  3. http://www.codeproject.com/KB/validation/aspnet_capcha.aspx (click on the correct image captcha)

Hope you find one that will work for you!

New Overload Method Available for String.Concat - available in .Net 4 Framework

Date Added: April 30, 2010 16:57 by By Edward
Categories: ASP.NET, Development Resources

A new feature I noticed while doing some prototyping some functionality with Visual Studio 2010, is the new overload method available for the 'String.Concat' method that takes an IEnumerable<T>. I found this to be very useful with LINQ query expressions.

Below is a simple example:

       

 public static void Main()
        {
            List<Employee> employees = new List<Employee>();

            employees.Add(new employees("John", "Doe"));
            employees.Add(new employees("Jane", "Doe"));
            employees.Add(new employees("John", "Code"));

            string output = String.Concat(employees.Where(employee =>
                          (employee.Surname == "Doe")));

            //write the output to screen
            Console.WriteLine(output);
        }


The Enumerable.Where extension method is called to extract the Employee objects whose 'Surname' property equals "Doe". The result is passed to the Concat<T>(IEnumerable<T>) method and displayed to the console.

 

String.IsNullOrWhiteSpace - New method available in .NET Framework 4

Date Added: February 19, 2010 14:11 by By Edward
Categories: ASP.NET, Development Resources

String.IsNullOrWhiteSpace - New method available in .NET Framework 4

I found a very nice addition to the .NET Framework(version 4) while experimenting with code in Visual Studio 2010. There is a new method called "IsNullOrWhiteSpace", which is more powerful than the more familiar "IsNullOrEmpty" method. Incase you were wondering, the "IsNullOrEmpty" method is still available to be used.

For example, the common whitespace symbol " " represents a blank space, as used between words and sentences. The most common whitespace characters may be typed via the space bar or the Tab key. Depending on context, a line-break generated by the Return key (Enter key) may be considered whitespace as well. This brings concerns when using the "IsNullOrEmpty" method.

Introducing "IsNullOrWhiteSpace"! In the .NET Framework 4, string.IsNullOrWhiteSpace() will return true if a string is full of whitespace characters. I found this very helpful when dealing with getting XML data from external web services.

Here is a short example of how to use this new method.

   1:          static void Main(string[] args)
   2:          {
   3:              // set some test values
   4:              string[] testValues = { null, String.Empty, "ABCDE", 
   5:                            new String(' ', 20), "  	   ", 
   6:                            new String(' ', 10) };
   7:              //loop through the list and return result
   8:              foreach (string value in testValues)
   9:              {
  10:                  Console.WriteLine("Return is: " + 
  11:              String.IsNullOrWhiteSpace(value));
  12:              }
  13:              // we want to see the return
  14:              Console.ReadLine();
  15:          }

 

Download Updated VS2010 and .NET 4 Training Kit

Date Added: January 12, 2010 14:41 by By Edward
Categories: ASP.NET, Development Resources, Technology

The updated VS2010 and .NET 4 Training Kit includes presentations, hands-on labs, and demos. Some of the features include:

  • C# 4.0
  • Visual Basic 10
  • F#
  • Parallel Extensions
  • Windows Communication Foundation
  • Windows Workflow
  • Windows Presentation Foundation
  • ASP.NET 4
  • Windows 7
  • Entity Framework
  • ADO.NET Data Services
  • Managed Extensibility Framework
  • Visual Studio Team System

You can download the training kit from the Microsoft website.

Come next month, Microsoft will offer testers yet another development milestone release of both Visual Studio 2010 and .NET Framework 4.

This version of the Training Kit works with Visual Studio 2010 Beta 2 and .NET Framework 4 Beta 2.

What is ahead for .Net in 2010?

Date Added: November 29, 2009 18:04 by By Edward
Categories: AJAX/JQuery, ASP.NET, Development Resources, Technology

We are almost at the end of 2009, and this year there have been some interesting things happening in the world of .Net and Microsoft. There was the release of Silverlight 3, Internet Explorer 8, updates to the AJAX library and toolkit, and just over a month ago we got Windows 7.

I thought I would point out a few new .Net technologies to look out for next year.

  • Visual Studio 2010 and ASP.Net 4.0: Microsoft already released beta versions of Visual Studio 2010 that will be running the ASP.Net framework 4.0. They aim to release the newest edition of Visual Studio and ASP.Net 4.0 on 22 March 2010.
  • MVC Framework: The first version of this framework has been released in March 2009. Currently MVC 2.0 Beta is available for download. Microsoft has made several changes to this framework since the first release. You should find the latest version available for download in the first half of 2010.
  • JQuery: It has taken Microsoft a while to wake up, but it is nice to know that JQuery is being adopted by Visual Studio. JQuery is a Javascript library that has a lot of neat tools in it's bag.  It is very helpful for taking care of mundane tasks like "get that div" or "set that text box value."  It also has a great set of methods for dealing with AJAX.  
  • WCF, WF and WPF: ASP.Net is a maturing framework that will continue to move forward, with or without you. The best advice I have is to get on the wagon. Tools and accompanying frameworks are maturing in line, but behind, the .NET framework - your applications should be as well.  The earlier you start, the better. WCF, or known as Windows Communication Framework, is used to abstract the "plumbing" of your application. It can save you hours and hours of coding.
  • Silverlight 4.0: Microsoft Silverlight is a web application framework that provides functionalities similar to those in Adobe Flash, integrating multimedia, graphics, animations and interactivity into a single runtime environment. Silverlight 4.0 Beta has been released last month, so do not be surprised to see the latest version available before June 2010.

Cleanup your code - remove unused using statements

Date Added: October 20, 2009 06:12 by By Edward
Categories: ASP.NET, Development Resources

One of the annoying things I have been dealing with in Visual Studio 2008 and Framework 3.5, is the unnecessary "using" statements the IDE adds, when you add a new webpage to your project.

Visual Studio does give you the option to remove these unused using statements, but they do not "tell" you about it.

Here is a quick way of removing unused using statements.

From the Context Menu:

  1. 1) Right-click anywhere inside the code editor,
  2. 2) point to Organize Usings,
  3. 3) now click Remove Unused Usings.


Here is an example:


Before:

   1:  using System;
   2:  using System.Linq;
   3:  using System.Collections.Generic;
   4:  using System.Text;
   5:  using System;
   6:   
   7:  namespace ConsoleApplication1
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              Console.WriteLine("DasCode.Net test page, removing 
unused using statements"
);
  14:          }
  15:      }
  16:  }


After:

   1:  using System;
   2:   
   3:  namespace ConsoleApplication1
   4:  {
   5:      class Program
   6:      {
   7:          static void Main(string[] args)
   8:          {
   9:              Console.WriteLine("DasCode.Net test page, removing 
unused using statements"
);
  10:          }
  11:      }
  12:  }

 

Compare Strings in C#

Date Added: September 25, 2009 05:55 by By Edward
Categories: ASP.NET

Comparing string values can be very frustrating as you would sometimes need to check for NULL values, the correct case, and empty strings. Because "string" can be NULL, one should always try avoiding the equality symbol. You should rather use the static String.Compare method. This method compare strings ignoring case(which is good for comparing user input), and compare strings using a specific culture. It can also handle NULL string references, which makes it the first choise method to compare strings.

For Example:


            string string1 = "DasCode.Net";
            string string2 = "DasCode";

            if (String.Compare(string1, string2) == 0)
            {
                // if true
                Response.Write("true");
            }
            else
            {
                // if false
                Response.Write("false");
            }

String.Compare also have several overload methods, which you can read more about here.

C# 4.0 - Response.RedirectPermanent

Date Added: September 21, 2009 13:48 by By Edward
Categories: ASP.NET, Technology

As discussed in an earlier post, ASP.NET 4.0 introduces some SEO improvements, to build SEO friendly sites, or make sites "more friendly" to search engines. A new feature available in the HttpResponse Object, is the "Response.RedirectPermanent" response.

If you were to use "Response.Redirect()" to direct from one page to another (Page 1 to Page 2), the search engine will keep the first page in their index. In case the original page (Page 1) no longer exists, and you want Page  to replace Page 1, you need to indicate it with a Response Code "301", and that is why Response.RedirectPermanent() method. This response sends out a 301 response message, which indicates that the Page has moved permanently. Response.RedirectPermanent() also has an overload with one parameter, incase you need to use it.

Here is a code example:

     public static void RedirectPermanent(this HttpResponse Response, string PathUrl) 
     { 
         Response.Clear(); 
         Response.Status = "301 Moved Permanently"; 
         Response.RedirectLocation = PathUrl; 
         Response.End();    
     } 

The ?? operator in C#

Date Added: September 18, 2009 05:50 by By Edward
Categories: ASP.NET

The "??" operator returns the left-hand operand if it is NOT NULL, otherwise it will return the right operand.

For example:

    int? i = null;
   
    // y = i, unless i is null, in which case y = -1.
    int y = i ?? -1;

The ?? operator also works with reference types:

    //message = param, unless param is null
    //in which case message = "No message"
    string message = param ?? "No message";

About DasCode.Net

I'm a ASP.NET web developer and code enthusiast. Blogging about everything .Net related.

Code... that's .net