December 26, 2010

Code Snippets in C# Useful for Beginners

1.)Below Is the Code Snippet To reverse The String


public static string ReverseString(string s)
    {
        char[] arr = s.ToCharArray();
        Array.Reverse(arr);
        return new string(arr);
    }

2.)Add The numbers Entered in a TextBox as String
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       
        <asp:TextBox ID="txtInputArray" runat="server"></asp:TextBox><br />
         <asp:Button ID="btnSum" runat="server" Text="SUM" OnClick="btnSum_Click" />
        </div>
    </form>
</body>
</html>
Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSum_Click(object sender, EventArgs e)
    {
        int result=AddNumbers(txtInputArray.Text);

        Response.Write(result);
    }

    public int AddNumbers(string str)
    {
        char[] Arr = str.ToCharArray();
        int ArrayLen = Arr.Length;
        int result = 0;

        for (int i = 0; i < ArrayLen; i++)
        {

            result = result + int.Parse (Arr[i].ToString ());
        }

        return result;

    }
}


December 25, 2010

Replacing \n line breaks with HTML line breaks in Gridview

I was Inputting values from textArea and showing it in a gridview .
I realized whenever I m Pressing Enter I m not getting the line break in GridView.
because while Saving values in DB from TextArea ,was saving Line Break in newline Char (\n)
which is Non-HTMl Char and won't be read in GridView.
So Below is the solution I used for replacing it with HTML Break (<br/>).

protected void gridHistory_RowDataBound(object sender, GridViewRowEventArgs e)
{
 GridViewRow row = e.Row;
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  row.Cells[0].Text = row.Cells[0].Text.Replace("\n", "<br />");
 }
}

December 20, 2010

Static /Private constructor


Static Constructors
Static Constructor is defined with keyword  static , It gets called only for one time after class
has been loaded .

Uses of static constructors. 
1.)If the class is static it can not be instantiated, In that case to initialize the variables we can
    write the static constructor 




Below is the example of calling static constructor

public static class EXPStaticConstructor
{


static int i;
    // Static constructor:
    static EXPStaticConstructor()
    {
        i = 10;
        System.Console.WriteLine("Initialized i in Static Constructor");
    }

    static void someMethod()
    {

        System.Console.WriteLine("The Some method invoked.");
        System.Console.WriteLine(i);


    }
}
    private class Teststatic
    {
        static void Main()
        {


            EXPStaticConstructor.someMethod();  //Before calling this method the static constructor will be called as We are first time using the class EXPStaticConstructor
 for calling the method 
        }
    }





Private Constructors
Instance constructors are public. If we change the access level of an instance constructor to private or if we haven’t specified any access modifiers at all , it becomes a private constructor
Like methods, default access level of a constructor is private.
It is not possible to inherit a class that has only private constructors.
Uses of private constructors. 
**********************
1. Private constructors are commonly used in classes that contain only static 
members.This helps to prevent the creation of an instance of a class by other classes when there are no instance fields or methods in that class.
2. Private constructors are useful when you want to create an instance of a class within
a member of the same class and not want to instantiate it out side the class.
3. Private constructors are used to create an instance of a class within a member of a nested class of the same class.


Below IS the Example of Calling Private Constructor from the inner class
  class Program
    {
        static void Main(string[] args)
             {
                     outer.Inner i = new outer.Inner();
        }
    }
     class outer
    {
        private outer()
        {
            System.Console.WriteLine("Private");
        }


        public  class Inner
        {
            public Inner()
            {
                new outer();    //calling private constructor of outer class
            }


         


        }
    }




Below is the example for calling Private Constructor withing the method
public class EXPPrivate
{
    static int i;
    private EXPPrivate()
    {

        i = 10;
    }

    public static void Create()
    {
        EXPPrivate obj = new EXPPrivate();    // this creates an object  and calls the private constructor

    }

    static void Main()
    {


        Create();

        System.Console.WriteLine(i);

    }
}