12 November 2011

File Creation Date and Time Conversion in C#


File Creation Date and Time Conversion in C#


using System;
using System.IO;
using System.Windows.Forms;

namespace ConvertDataTime
{
    public partial class ConvertDataandTime : Form
    {
        public ConvertDataandTime()
        {
            InitializeComponent();
        }

   
        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                //"08/26/2011 15:00:00"
                FileSystemInfo f = new FileInfo(@txtPath.Text);
                f.CreationTime = Convert.ToDateTime(txtDateTime.Text);
                f.LastWriteTime = Convert.ToDateTime(txtDateTime.Text);
                f.LastAccessTime = Convert.ToDateTime(txtDateTime.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtDateTime.Text = "08/26/2011 15:00:00";
            txtPath.Text="D:\\";
        }

     

     
    }
}


11 November 2011

Composition vs Inheritance in OOPS


 Composition vs Inheritance in OOPS

Composition – Composition is used when the two class has-a relationship among classes
Inheritance – Inheritance is used when the derived class is-a relationship base class

For example :
Take two class
1.House owner
2.House

House owner is a House is not valid - Inheritance
House owner has a house is valid - Composition

For example :
Take two class
1.Game
2.Cricket


Cricket is a game is valid - Inheritance
Cricket has a game is invalid - Composition

When to Use Abstract class?


    When to Use Abstract class?

·         If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. 
·         Abstract classes should be used primarily for objects that are closely related.
·        If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components

10 November 2011

When to use Interface in C#?

When to use Interface  in C#?



·         Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
·        Structures cannot inherit from classes, but they can implement interfaces.
·         Interfaces are best suited for providing common functionality to unrelated classes.
·    If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. 

Suppose a business man has two accounts:
1.       Business Account
2.        Personal Account
You might create a standard int
erface named IAccount, which included PostInterest and DeductFees methods:
// C#
// Code for the IAccount interface module.
public interface IAccount
{
   void PostInterest();
   void DeductFees();
}

Both Business Account and Personal Account inherit the above interface and implement the method. Both Classes have implement two function and the function change frequently according to bank policies. In those situations Interface is used.
When the function frequently changing and the common function is used for unrelated class, Interfaces is used

References:

Get the Selected row value in Gridview C#


Get the Selected row value in Gridview C#

string cellValue = mydatagrid["columnName",rowindex].Value.ToString();


ConnectionString for Sql Server authentication and Windows Authentication

ConnectionString for Sql Server authentication

      public string connStr = "Server=USER-PC\\SQLEXPRESS;Database=StudentDetails;UId=sa;pwd=sa;Integrated Security=false";



ConnectionString for Windows Authentication



 public string connStr = "Server=USER-PC\\SQLEXPRESS;Database=StudentDetails;Integrated Security=true";

7 November 2011

Delegates in C#

Delegates in C#


A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++.


Declaring Delegates



public delegate void Del(string message);
Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method.



// Create a method for a delegate.
public static void DelegateMethod(string message)
{
    System.Console.WriteLine(message);
}

// Instantiate the delegate.
Del handler = DelegateMethod;

// Call the delegate.
handler("Hello World");

Sources : http://msdn.microsoft.com/en-us/library/ms173172(v=vs.80).aspx

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...