Windows Phone Developers

Sunday, April 11, 2010

How to Add an Array to List using C# /.NET

Add Range to List using C# / .NET

Let us take our own IPL example. The current IPL has eight teams that are part of the list

            List lstIPLTeams = new List();

            lstIPLTeams.Add("Mumbai Indians");
            lstIPLTeams.Add("Chennai SuperKings");
            lstIPLTeams.Add("Kolkatta KnightRiders");
            lstIPLTeams.Add("Deccan Chargers");
            lstIPLTeams.Add("Rajastan Royals");
            lstIPLTeams.Add("Delhi DareDevils");
            lstIPLTeams.Add("KingsXI Punjab");
            lstIPLTeams.Add("RC Bangalore");




From next year two new teams- Pune and Kochi - will be part of IPL

Hence we are adding those two teams to an Array and adding the array using AddRange method

            string[] NewIPLTeams = { "Pune", "Kochi" };
            lstIPLTeams.AddRange(NewIPLTeams);

            Console.WriteLine("\nList After Add Range");
            Console.WriteLine("=======================");
            foreach (string IPL in lstIPLTeams)
            {
                Console.WriteLine(IPL.ToString());
            }


This will add Pune and Kochi to end of the list Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to Add and Sort Items in a list using C#

Adding and Sorting List Items using .NET (C#)

The following example creates a Generic list of IPL teams and sorts them


The following code does it:

List lstIPLTeams = new List();

            lstIPLTeams.Add("Mumbai Indians");
            lstIPLTeams.Add("Chennai SuperKings");
            lstIPLTeams.Add("Kolkatta KnightRiders");
            lstIPLTeams.Add("Deccan Chargers");
            lstIPLTeams.Add("Rajastan Royals");
            lstIPLTeams.Add("Delhi DareDevils");
            lstIPLTeams.Add("KingsXI Punjab");
            lstIPLTeams.Add("RC Bangalore");


            Console.WriteLine("List Before Sorting");
            Console.WriteLine("=======================");
            foreach (string IPL in lstIPLTeams)
            {
                Console.WriteLine(IPL.ToString());
            }

            //Sort the Items
            lstIPLTeams.Sort();

            Console.WriteLine("\nList After Sorting");
            Console.WriteLine("=======================");
            foreach (string IPL in lstIPLTeams)
            {
                Console.WriteLine(IPL.ToString());
            }

Here is the output



Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Friday, April 9, 2010

Private Constructors in C# / VB.NET

Use of Private Constructors in C# (.NET)

Here is an example of a Private Constructor

public class WicketCounter
    {
        //Private Constructor
        private WicketCounter() { }

        // No of Wickets
        public static int Wickets;
        // Public Method to Return the No of Wickets
        public static int WicketCount()
        {
            return ++Wickets;
        }

     
    }


The Class has a private constructor, a public static method and a static variable. The Method increments the Wicketcount (and hence the class itself is called wicketcounter) and the value is returned through Wickets var

WicketCounter.WicketCount();
            
            MessageBox.Show(WicketCounter.Wickets.ToString());


The declaration of private constructor prevents the creation of default constructor. Hence the class cannot be instantiated and doesn;t contain any instance fields or methods Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to Download File from Website using C# (.NET)

 How to Download File from Internet using C# (.NET)


private bool download_file_from_Net()

        {
            try
            {
                WebClient WC = new WebClient();
                String sWebLocation = "http://logic.stanford.edu/talks/Web2.0/microsoft-logo.jpg";
                String sLocalPath = "C:\\Temp\\New folder (2)\\microsoft-logo.jpg";
                WC.DownloadFile(sWebLocation, sLocalPath);
                return true;
            }

           catch (Exception ex1)
            {
                return false;
            }
        }
  



Following errors might occur if you do not use the Destination File Name

For example


                String sLocalPath = "C:\\Temp\\New folder (2)";


throws the following error
 InnerException = {"Access to the path 'C:\\Temp\\New folder (2)' is denied."}

and


                String sLocalPath = "C:\\Temp\\New folder (2)\\";



 throws the following one


System.Net.WebException was caught
  Message=An exception occurred during a WebClient request.
  InnerException: System.IO.DirectoryNotFoundException

       Message=Could not find a part of the path 'C:\Temp\New folder (2)\'.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Monday, April 5, 2010

How to Bind a Lisbox to Database in C# (.NET)

Bind Listbox to Dataset using Visual Studio

Following steps would help you in connected a database/dataset to the list box

Step1: Add a List Box control to the form and select the DataSource from quick edit

Step 2: Select appropriate data source type (We are going to connect to an SQL Server DB)


Step 3: Select Appropriate Database Model


Step 4: Add a connection to database and select appropriate DB

Step 5: Select appropriate field you want to show in listbox


A dataset will be created and will be used for it. You can write necessary code in listbox selectionchange events depending upon the business logic Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Switching Application Types using Visual Studio

Converting Application Types using Visual Studio

Visual Studio helps you to change Console Application to Windows Forms Application as shown below. This will help if you want to have your output on Console; then you can change the type from WinForms to console and view the o/p.

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Saturday, April 3, 2010

Debug.Print and Debug.Assert in C#

VB Programmers would have used Debug.Print and Debug.Assert at will while debugging the code. These two statements come handy for efficient debugging of the code.

This can be done in C# using System.Diagnostics


using System.Diagnostics;


missing the above directive will cause 'The name 'Debug' does not exist in the current context" error



int i1 = 0;
while (i1 < 10)
{
Debug.Assert(i1 != 3);
++i1;
}


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon