using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Net3p5Features.Expression
{
public class AnonymouseMethodTest : IProgramTest
{
public string Name
{
get { return "Anonymouse Sample"; }
}
public string Description
{
get { return Name; }
}
public void Run()
{
var car = new Car
{
Brand = "BMW",
Model = "92",
OnRunning =delegate(object sender,EventArgs e)
{
Console.WriteLine("sender :{0} called from Anynomouse method", sender);
var s = "Hello world";
}
};
car.Run();
var car2 = new Car
{
Brand = "BMW",
Model = "92",
OnRan =
(sender, e) =>
{
Console.WriteLine("sender :{0} called from Anynomouse method", sender);
var s = "Hello world";
}
};
car2.Run();
}
}
public class Car
{
public string Brand { get; set; }
public string Model { get; set; }
public EventHandler OnRunning;
public EventHandler OnRan;
public void Run()
{
if (OnRunning != null) OnRunning(this, EventArgs.Empty);
Console.WriteLine("Model:{0}, Brand:{1}", Model, Brand);
if (OnRan != null) OnRan(this, EventArgs.Empty);
}
}
}
Saturday, September 18, 2010
Anonymous Methods
Generics
public delegate bool Filter(T dinfo); protected void TestExtensionWithLambda(){
Console.WriteLine("Enter the Min number of nested files:");
var noS = Console.ReadLine();
//IEnumerabledirectories = GetDirectories().OrderBy(d=>d.GetDirectories().Length);
IEnumerable<DirectoryInfo> directories = from d in GetDirectories()
orderby d.GetFiles().Length descending
select d;
var result = directories.Filter(d => d.GetFiles().Length >= int.Parse(noS));
Console.WriteLine("Original Collection={0}", directories.Count());
foreach (var dInfo in result)
{
Console.WriteLine("Name={0}, Dirctory Count={1}", dInfo.Name, dInfo.GetFiles().Length);
}
}protected static IEnumerable<DirectoryInfo> GetDirectories()
{
var dInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
var dInfos = dInfo.GetDirectories("*", SearchOption.AllDirectories);
var list = from d in dInfos
select d;
return list;
}
Friday, September 17, 2010
OOP Javascript
//Simple OOP Sample
// in this Sample
// in this Sample I will Create objects like C# objects
// Hint all this code support Intellisense
function RunSampleA() {
var Car = new Object();
var Bus = new Object();
Bus.Car = Car;
Car.Name = "BMW";
Car.Width = 20;
Car.Size = { Width: 30, Height: 50 }; // like JSON
Car.Height = 50;
Car.Play = function () {
return this.Name + '; ' + this.Width.toString() + '; ' + this.Height.toString();
}
Car.Play2 = function () {
return this.Name + '; ' + this.Size.Width + '; ' + this.Size.Height;
}
alert(Car.Play());
alert(Car.Play2());
alert(Bus.Car.Play());
}
// Initialising and creating nested objects.
function RunSampleB() {
var ServiceManager = { CarService: 'My Car Service', ComputerService: 'My Computer Service',
FoodService: { DesertServices: "my desert service", Dinners: "Dinner Foods", "DoService": function (food) {
alert('Cooking ' + food);
}
}
};
alert(ServiceManager.CarService);
alert(ServiceManager.FoodService.DesertServices);
alert(ServiceManager.FoodService.Dinners);
alert(ServiceManager.FoodService.DoService('Yamme rice'));
}
//Object Model Sample with call back methods
function RunSampleC() {
var ServiceManager = new Object();
ServiceManager.CarService = 'My Car Service';
ServiceManager.ComputerService = 'My Computer Service';
ServiceManager.FoodService = new Object();
ServiceManager.FoodService.Dinners = "My Dinner";
ServiceManager.FoodService.DesertServices = "My Desert service";
ServiceManager.FoodService.DoService = function (food) {
alert('Cooking ' + food);
return 1;
}
ServiceManager.FoodService.DoCallbackService = function (food, cb) {
cb(food);
return 1;
}
alert(ServiceManager.CarService);
alert(ServiceManager.FoodService.DesertServices);
alert(ServiceManager.FoodService.Dinners);
// alert(ServiceManager.FoodService.DoService('Yamme rice'));
alert(ServiceManager.FoodService.DoCallbackService('rice', callBack));
}
function callBack(data) {
alert(data + ' was called from call back');
}
// Creatig objects that can be initialised with constructor
function RunSampleD() {
var Dog = function (id, name, age, color) {
this.Color = color;
this.Age = age;
this.ID = id;
this.Name = name;
this.toString = function () {
return this.ID + '; ' + this.Name + '; ' + this.Age + '; ' + this.Color;
}
this.Bark = function () { alert('Hoow, Hoow'); }
}
var buddy = new Dog(1, 'Buddy', 3, 'White');
alert(buddy);
buddy.Bark();
}
Monday, September 6, 2010
ASP.NET 4 & MVC2
Hint replace all $ by %
if the code will be run on the server but not renders
use this format
<$ sever code block ; $>
if the code will render output
use
<$ =sever code block (without;) $>
for encoded Html
<$ :sever code block (without;) $>
Tuesday, July 6, 2010
Subscribe to:
Posts (Atom)