Archive for category: C#

Imagine Cup SriLanka 2012

Imagine Cup SriLanka 2012

Hi Folks,

Imagine Cup 2012

It’s competition time. ImagineCup Srilanka 2012 competitions are kicked off. Most of the universities submitted their proposals with awesome ideas. Next round is going to be an inter university competition to short list the teams with in the university. Hope to see some amazing ideas and concepts evolving this time as well. Stay tuned for updates.

February 17, 2012 0 comments
Just A Try ;)

Just A Try ;)

Hi all,First of all welcome to my Blog. This is just a try to tell things which i found difficult when i was doing those. So enough talking.cialis online Let’s get into business. I’ll catch you with a useful post.

January 6, 2012 Comments are Disabled
Extension Methods in C#

Extension Methods in C#

Hi Folks,

Just heard about a concept called Extension methods in C# and decided to share that with you all.  As the name implies extension methods adds extra functionality to a class implementation which can’t be modified now. For example if you are using an API and you need to expand some existing functionality. Can you do it ? Yes. You can with Extension Methods.

Let’s learn with example.

Now let’s create a class called “Products” with “displayProduct()” method. Now let’s assume we don’t have control to change the above class modification but we need to add a “printProduct()” functionality to the existing products.

Our Basic Products Class

 public class Products
    {
        public string Name
        {
            get;
            set;
        }

        public void displayProduct()
        {
            //Display the product
            Console.WriteLine("Displaying the Product");
        }
    }

And this is how we implemented the new functionality.

public static class ProductExtension
    {
        public static void printProduct(this Products pro)
        {
            //Print Product
            Console.WriteLine("Printing the Product");
        }
    }

Create a static class and add “printProduct()” functionality as a static method.

Now you can access Display and Print functionality as always.

class Program
    {
        static void Main(string[] args)
        {
            Products MyProduct = new Products();
            MyProduct.displayProduct();
            MyProduct.printProduct();
            Console.Read();
        }
    }

C# Roxxxx.
Happy Coding :)

December 12, 2011 1 comment
Mongo DB & C# – Fun and Fun Only

Mongo DB & C# – Fun and Fun Only

MongoDB, As I said in my title it’s just fun to deal with. Believe me you also will say the same at the end of this post. So let’s go into NO SQL World.

What is No SQL ?

No more sql here pls. This is a world without SQL. So how to perform operations with db ?. Follow with me……

What is Mongo DB ?

Mongo DB is a NO SQL Database server. Most of the popular programming languages got drivers to connect and play with Mongo. No SQL is a new world of storage. You don’t find any tables or rows to store your data. So How ? There is a different representation to store data. We don’t have tables anymore but we have something called collections. We don’t have Rows or Records anymore bu we have something called documents. Basically if you want to store a bunch of contact details, you first create a ‘Contacts’ collection and store each contact detail as a ‘Contact’ document.

Let’s get started!!!

Downloading Mongo DB

1 ) Take a quick look in to Quick Start Guide to install the DB.
2) It’s good to install as a Windows service. Execute the following command to install as a windows service.

D:\mongodb\bin>mongod –dbpath D:\mongodb –logpath D:\mongodb\log.txt –install

3) Start the service.
4) Download C# Driver from the following location.

     http://github.com/samus/mongodb-csharp/tree/master

There are several drivers available for C#. I’m going to use this one in this tutorial.

Connection to DB with C# Driver

Create a new C# project (Console Project) and add reference to C# Driver you already downloaded.

Mongo mongo = new Mongo(ConnectionString);
mongo.Connect();
IMongoDatabase db = mongo.GetDatabase("Test");

That’s it. We Connected to DB called ‘Test’.

Let’s say which collection we are going to use.

IMongoCollection Contacts = db.GetCollection("Contacts");

Well, we also said we are going to use Collection ‘Contacts’.

Now we need to insert a new Record. Oh Sorry ! No more records. We need to insert a ‘Document’.

   Document doc = new Document();
   doc["Name"] = "John";
   Contacts.Insert(doc);
   mongo.Disconnect();

So now we inserted a document with a ‘Name’ property. The cool things about NO SQL DB and Documents is it’s schema free. Now we can insert another document to the same collection with another property called ‘id’ or something.

Full Source code of the above example would look like

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

namespace MongoDB
{
    class Program
    {
        static void Main(string[] args)
        {
            Mongo mongo = new Mongo();

            mongo.Connect();

            IMongoDatabase db = mongo.GetDatabase("Test");

            IMongoCollection Contacts = db.GetCollection("Contacts");

            Document doc = new Document();

            doc["Name"] = "John";

            Contacts.Insert(doc);
            mongo.Disconnect();
            Console.WriteLine("Inserted");
            Console.ReadLine();

        }
    }
}

Using Connection String

You also can use connection string to connect to a specific Mongo DB server.

string ConnectionString = "YourConnectionStringHere";
Mongo mongo = new Mongo(ConnectionString);

Mongo DB Hosting

There are several mongoDB hosting services available. https://mongohq.com/home is a good service which has a free star up package to test MongoDB Application. You can use connection string to connection to these Hosted MongoDBs.

November 8, 2011 1 comment
Simple Silverlight Image Editor – Snap Tweak 1.0

Simple Silverlight Image Editor – Snap Tweak 1.0

Simple and Friendly interface With Basic Image Manipulations

Hi all,

I’m always passionate about fancy stuffs in programming and WPF is a ultimate platform to go crazy with fancy in C#. Silverlight opens so many ways to create amazing web components and interfaces. So finally i decided to use Silverlight to build a simple Photo Editing Tools which is capable of doing some basic image manipulations. You can see the demo  of  Snap Tweak 1.0.

Snap Tweak 1.0 Capable of

  • Converting Image Formats (Jpeg, Png)
  • Rotating
  • Applying Image Filters
You can see more functionalities in future versions.
This is just a quick post to introduce the working copy. I’ll try to cover the implementation details and publish the source code in another post.
Happy Image Editing :)
September 25, 2011 0 comments