C#
  Home arrow C# arrow Page 3 - Coding Skills for a Windows Form Applicati...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Coding Skills for a Windows Form Application
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-06-29

    Table of Contents:
  • Coding Skills for a Windows Form Application
  • How to code with a readable style
  • How to code comments
  • How to work with the Text Editor toolbar
  • How to use code snippets

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Coding Skills for a Windows Form Application - How to code comments


    (Page 3 of 5 )


     
    Comments are used to document what the program does and what specific blocks and lines of code do. Since the C# compiler ignores comments, you can include them anywhere in a program without affecting your code. Figure 3-8 shows you how to code two types of comments.

    First, this figure shows a delimited comment at the start of a method. This type of comment is typically used to document information that applies to an entire method or to any other large block of code. You can include any useful or helpful information in a delimited comment such as a general description of the block, the author’s name, the completion date, the files used by the block, and so on.

    To document the purpose of a single line of code, you can use single-line comments. Once the compiler reads the slashes (//) that start this type of comment, it ignores all characters until the end of the current line. In this figure, single-line comments have been used to describe each group of statements. In addition, single-line comments have been used at the end of some lines of code to clarify the code.

    Although many programmers sprinkle their code with comments, that shouldn’t be necessary if you write your code so it’s easy to read and understand. Instead, you should use comments only to clarify code that’s difficult to understand. The trick, of course, is to provide comments for the code that needs explanation without cluttering the code with unnecessary comments. For example, an experienced C# programmer wouldn’t need any of the comments shown in this figure.

    One problem with comments is that they may not accurately represent what the code does. This often happens when a programmer changes the code, but doesn’t change the comments that go along with it. Then it’s even harder to understand the code, because the comments are misleading. So if you change the code that has comments, be sure to change the comments too.

    Incidentally, all comments are displayed in the Code Editor in a different color from the words in the C# statements. By default, the C# code is blue and black (blue for C# keywords and black for the rest of the code), while the comments are green. That makes it easy to identify the comments.

    A method with comments

      private void btnCalculate_Click(object sender, System.EventArgs e)
    {
         /**********************************
         * this method calculates the total
         * for an invoice depending on a
        
    * discount that’s based on the subtotal   
         **********************************/

         // get the subtotal amount from the Subtotal text box
         decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);

         // set the discountPercent variable based
         // on the value of the subtotal variable
         decimal discountPercent = 0m; // the m indicates a decimal value
         if (subtotal >= 500)
         {
           
    discountPercent = .2m;
         }
         else if (subtotal >= 250 && subtotal < 500)
         {
           
    discountPercent = .15m;
         }
         else if (subtotal >= 100 && subtotal < 250)
         {
           
    discountPercent = .1m;
         }
        
    // calculate and assign the values for the
         // discountAmount and invoiceTotal variables
         decimal discountAmount = subtotal * discountPercent;
         decimal invoiceTotal = subtotal - discountAmount;

         // format the values and display them in their text boxes
         txtDiscountPercent.Text =     // percent format 
            discountPercent.ToString("p1");                            // with 1 decimal place
         txtDiscountAmount.Text =
            discountAmount.ToString("c");                             // currency format
         txtTotal.Text =
            invoiceTotal.ToString("c");

         // move the focus to the Subtotal text box
         txtSubtotal.Focus();
      }

    Description

    • Comments are used to help document what a program does and what the code within it does.
    • To code a single-line comment, type // before the comment. You can use this technique to add a comment on its own line or to add a comment at the end of a line.
    • To code a delimited comment, type /* at the start of the comment and */ at the end. You can also code asterisks to identify the lines in the comment, but that isn’t necessary.

    Figure 3-8   How to code comments

    More C# Articles
    More By Murach Publishing


       · This article is an excerpt from the book "Murach's C# 2005," published by Murach. We...
     

    Buy this book now. This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway