Friday, May 9, 2014

Text to Speak windows form application in C#

Open Visual Studio.
File-->New Project.
In the dialog box, choose windows forms application.
Form1.cs design page will be open.
After that design a form listed below:


Right click on Project-->Add Reference--->Then select System.Speech and click OK.



Then double click on Speak button and write code listed below:

//add new namespace 

using System.Speech.Synthesis;

Then :-


private void button1_Click(object sender, EventArgs e)
        {
            SpeechSynthesizer s = new SpeechSynthesizer();
            s.Speak(textBox1.Text);
        }

Now run the application and enjoy. :P

Saturday, March 1, 2014

Simple example of Interface in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpInterface
{
    interface mahi
    {
        void set();
    }
    class mahi1 : mahi
    {
        public void set()
        {
            Console.WriteLine("Hate the word LOVE");
            Console.ReadLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            mahi1 m = new mahi1();
            m.set();
        }
    }
}

Output:

Simple example of Inheritance in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleInheritance
{
    class shape
    {
        public int height;
        public int width;
        public void setHeight(int h)
        {
            height = h;
        }
        public void setWidth(int w)
        {
            width = w;
        }
    }
    class Area : shape
    {
        public int getArea()
        {
            return height * width;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Area a = new Area();

            a.setHeight(5);
            a.setWidth(5);

            Console.WriteLine("Area of Rectangle : {0}", a.getArea());
            Console.ReadLine();
        }
    }
}

Output:

Example of C# Structure.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpStruct
{
    struct Books
    {
        public string title;
        public string author;
        public string subject;
        public int id;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Books b1, b2;

            b1.title = "Little Inspiration";
            b1.author = "Mahi";
            b1.subject = "Quotes";
            b1.id = 5;

            Console.WriteLine("Book 1 title : {0}", b1.title);
            Console.WriteLine("Book 1 author : {0}", b1.author);
            Console.WriteLine("Book 1 subject : {0}", b1.subject);
            Console.WriteLine("Book 1 book_id :{0}", b1.id);

            Console.WriteLine();

            b2.title = "Suvichar";
            b2.author = "Jignesh Trivedi";
            b2.subject = "Quotes";
            b2.id = 10;

            Console.WriteLine("Book 2 title : {0}", b2.title);
            Console.WriteLine("Book 2 author : {0}", b2.author);
            Console.WriteLine("Book 2 subject : {0}", b2.subject);
            Console.WriteLine("Book 2 book_id : {0}", b2.id);

            Console.ReadLine();
        }
    }
}

Output:


Example of String.Compare() in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StringCompare
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "Hello World";
            string b = "Hello Friends";

            if(String.Compare(a,b)==0)
            {
                Console.WriteLine(a+" and "+b+" are equal");
            }
            else
            {
                Console.WriteLine(a+" and "+b+" are not equal");
            }
            Console.ReadLine();
        }
    }
}

Output:

Example of Type Coversion.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TypeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = 2500.25;
            int a;

            a = (int)d;
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}

Output:

Thursday, February 27, 2014

Program of Method Overloading.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MethodOverloading
{
    class test
    {
        public void show()
        {
            Console.WriteLine("No Parameter");
        }
        public void show(int a)
        {
            Console.WriteLine("One Parameter " + a);
        }
        public int show(int a, int b)
        {
            Console.WriteLine("Two Parameters are " + a + " and " + b);
            return a + b;
        }
        public double show(double a, double b)
        {
            Console.WriteLine("Two Parameters are " + a + " and " + b);
            return a + b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int sum;
            double sum1;
            test t = new test();
            t.show();
            t.show(10);
            sum = t.show(10, 20);
            Console.WriteLine(sum);
            sum1 = t.show(10.5, 20.5);
            Console.WriteLine(sum1);
            Console.ReadLine();
        }
    }
}

Output: