Archive

Posts Tagged ‘C#’

What about the Virtual Keyword in C#

January 28th, 2010 No comments

What confuses sometimes is the use of virtual keyword, so let’s keep some notes in order to remember it.

Definition
The virtual keyword is used to modify a method or property declaration, in which case the method or the property is called a virtual member. The implementation of a virtual member can be changed by an overriding member in a derived class.

Have in mind
You cannot use the virtual modifier with the following modifiers: static, abstract, override.

How to use it
A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

When to use it
When you want to support polymorphism. When objects derive from a common class, having some common behavior but there are some small differences at the implementation of each object’s behavior.

Examples

using System;
class TestVirtual
{
   public class Shape
   {
      public const double pi = Math.PI;
      protected double x, y;
      public Shape() { }
      public Shape (double x, double y)
      {
         this.x = x;
         this.y = y;
      }

      //This method can be overriden
      public virtual double Area()
      {
         return x*y;
      }
   }

   public class Circle: Shape
   {
      public Circle(double r): base(r, 0)
      {
      }

     // This method is being overriden
      public override double Area()
      {
         return pi * x * x;
      }
   }

	public class Rectangle: Shape
	{
		public Rectangle(double x, double y): base(x, y){}
	}

   public static void Main()
   {
      double r = 3.0, h = 5.0;
      Shape c = new Circle(r);
      Shape s = new Rectangle(r, h);

      // Display results:
      Console.WriteLine("Area of Circle   = {0:F2}", c.Area());
      Console.WriteLine("Area of Rectangle   = {0:F2}", s.Area());
   }
}

Results
Area of Circle: 28.27
Area of Rectangle: 15

Categories: C# Tags: , ,

JSON.NET Library

January 13th, 2010 No comments

Json.NET is a lightweight library that allows you to convert classes from .NET to JSON and back again. One of the key features is that allows you to use LINQ for reading and writing JSON. JSON in contrast to XML offers a very lightweight way to feed javascript objects in order to make ajax calls as light as possible

Some Key Features:

  • Flexible JSON serializer to convert .NET objects to JSON and back again
  • LINQ to JSON for reading and writing JSON
  • Writes indented, easy to read JSON
  • Convert JSON to and from XML
  • Supports Silverlight and the Compact Framework

Here is an example of how easy it is to use it:

Article article = new Article
{
ID = 1,
Title = "Article Test 1",
InsDate = DateTime.Now,
Authors = new string[] { "author1", "author2" }
};

Article deserializedArticle = JsonConvert.DeserializeObject<Article>(output);

And the output is:

{
ID": "1",
"InsDate": "\/Date(1292868060000)\/",
"Title": "Article Test 1",
"Authors": [
"author1",
"author2"
]
}
Categories: Libraries Tags: ,

How to Create an Event in ASP.NET User Control

October 26th, 2009 No comments

Let’s assume we have a control ShippingSelect.ascx and we would like to assign an event like ItemCommand.

Firstly we add a new User Control to our project (ShippingSelect.ascx) and at the code behind we need to declare the name of the event using the EventHandler<TEventArgs> where TEventArgs is an EventArgs that contains the event data.

Read more…

Categories: ASP.NET Tags: ,