Monday, November 25, 2013

Generic functionality and sample program.

Generics refers to a technique of writing code for a class without specifying the data type that the class works with.

First we should now why we required generic

Earlier in C# 1.1 we have object programming and there we  can create object  and in case of typesafe and performace we were creating another classes.

In generic basically we are defining the datatype.

Let say.
  class Generic
    {
        T[] itemsType;
        public static string Name ="test";
    }
}
Calling the generic function
   String nameValue = "";
            nameValue = Generic<string>.Name;

Simple generic program:-
Create a class below
class Address {

        public int SrNo { get; set; }
        public string Name { get; set; }

        public Address(int srNoInput, string NameInput)
        {
            this.SrNo = srNoInput;
            this.Name = NameInput;
        }

    }

Use this class through generic functionality

       List<Address> Addresslist = new List<Address>();

            Addresslist.Add( new Address(1,"Fist"));
            Addresslist.Add( new Address(2,"Second"));
            Addresslist.Add( new Address(3,"Third"));

            foreach( Address Add in Addresslist)
            {

                Console.WriteLine("Address Number:- "  +  Add.SrNo);
                Console.WriteLine("Address Name r:- " + Add.Name);


            }



Friday, November 8, 2013

What is an Interface:-

Good example for this would be like a computer hardware engineer who does not have to know how a The circuit board is designed in the hard disk or the mother board as long as he Knows that if he replaced it with a new one with the correct specification, it Would work perfectly.
This is Really a robust feature in software development.
Same you can write an interface and send to the developer To implement all this.

·         Setup strict restriction on the application.
·         Same as inheritance and encapsulation can be done with This.
·         Mostly like pure abstract class which don’t have Implementation.
·         A class can have multiple interface but single base class
·         Loosely coupled application, you know we can create Interfaces and change in the application, the interface can be used in another project Too.
·         In application architecture, roll can be defined in the Developer, work can start parallel like I have created an interface for my web Application, designer can start work on this and same time developer can start Wring the base logics.
·         Ease to implement new features and reusability.

Defining And interface in an application and how we will use this.
1.       Add a class library project and give it the name as “SearchBook”
2.       Interface name should be like ISearchBook, The prefix should start with “I”
This will looks like below
  interface ISearchBook
    {
        List<string> GetSearchResult(String searchPharse);   
    }
Below is the sample code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SampleWindow
{
    public partial class Form1 : Form ,ISearchBook
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            System.Console.Write("Here Interface invoked");    
            InterfaceGetName("test");
            List<string> Result = GetSearchResult("1");
           
        }
 
        public void InterfaceGetName(string Name)
        {
            System.Console.Write(Name);
        }

        public List<string> GetSearchResult(String searchPharse)
        {
           
            List<string> Books = new List<string> { "1", "2", "3", "4" };
            List<string> Result= new List<string>();

            Result = (from A in Books where A.StartsWith(searchPharse) select A).ToList();
           
            return Result;
        }
    }

    interface ISearchBook
    {
        List<string> GetSearchResult(String searchPharse);   
    }
  
}

Implementing interfaces details with practical examples
1.       Frist I created new project in VS 2010. Empty project, Added new page(home.aspx), added new link lnkInterfaces when running the project it gives me error.
handle clause requires a withevents variable
Slution: declared the withevent for linkbuttion  where class strat(Inside class)
 Private WithEvents lnkInterfaces As LinkButton
2.       Added New window application, then added new interface on this through new item
namespace SampleWindow
{
    interface Interface1
    {
        int test;
    }
}
This was giving some exception that
Error       1              Interfaces cannot contain fields
Open Question :-  Why interfaces cannot contain fields ??
3.       We can create a property over there
int A { get; set; }
4.       We cannot add a private access modifier this will give below exception

The modifier 'private' is not valid for this item              

Sample interface
  interface Interface1
    {
       
        void InterfaceGetName(string Name);
     

    }

Used in class
    public partial class Form1 : Form ,Interface1
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            System.Console.Write("Here Interface invoked");
    
            InterfaceGetName("test");
           
        }
 
        public void InterfaceGetName(string Name)
        {
            System.Console.Write(Name);
        }
    }

Point : - if we inherit the interface in a class then all the function need to be implemented in the derived class.

Wednesday, October 16, 2013

Converting time zone in asp.net,C# and vb.net through framework 3.5.

First add below references.

Namespace:  System
Assembly:  System. Core (in System. Core. dell)

Sample function that works for all of you.

Create below function in common classes.

  Public Function GmtToPacific (ByVal dateTime As DateTime) As DateTime
        Return TimeZoneInfo. ConvertTimeFromUtc (dateTime, TimeZoneInfo. FindSystemTimeZoneById ("Pacific Standard Time"))

    End Function

Calls this function and pass input date in UTC/GMT format.

Dim objName as new CommonClassName

objName .GmtToPacific(sinceDate.ToUniversalTime.ToString())


How to consume RaaS service( Report as a service), details and sample code

Dim webRequest As HttpWebRequest = DirectCast(Net.WebRequest.Create("URL of Raas service"), HttpWebRequest)
        webRequest.Method = "GET"
        webRequest.ContentType = "application/x-www-form-urlencoded"
        webRequest.Credentials = New NetworkCredential("URL of RaaS service", "token")
        Dim httpResponse As HttpWebResponse
        Dim contentReader As StreamReader
        Try
            httpResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
            contentReader = New StreamReader(httpResponse.GetResponseStream())
            Dim readToEnd As String = contentReader.ReadToEnd()
        Catch ex As Exception
            '' Log the exception here
        End Try