Thursday, July 17, 2008

Wednesday, July 16, 2008

List Comprehension in C# with LINQ - part 2

In this example, I tried to figure how to use
a static method to filter an array

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

namespace LINQArray
{
class Program
{
public static bool filter(string s)
{
bool ret = false;
if (s.StartsWith("A"))
{
ret = true;
}
return ret;
}

static void Main(string[] args)
{
string[] countries = { "Britain", "France", "Croatia", "Argentina", "Australia", "Mexico", "Finland",
"Spain", "Italy", "Greece" };
var thecountries = from c in countries
where filter(c)
select c;
foreach (var c in thecountries)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
}
}

List Comprehension in C# with LINQ

List comprehensions from Python (from Haskell) miss me a lot in C#
Happily with LINQ I dont miss then anymore :

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

namespace ListComprehension
{
class Program
{
static void Main(string[] args)
{
string[] countries = { "Britain", "France", "Croatia", "Argentina", "Australia", "Mexico", "Finland",
"Spain", "Italy", "Greece" };

string[] favcountries = { "Britain", "Argentina", "Greece", "Brazil" };

var thecountries = from c in countries
where favcountries.Contains(c)
select c;

foreach (var c in thecountries)
{
Console.WriteLine(c);
}
Console.ReadLine();
}
}
}

LINQ - .NET

LINQ stands for "Language Integrated QUery"
This really great feature can drastically ease SQL request.
Here is an example :

var products = from p in db.Products
where p.Category.Categoryname == "Beverages"
select p;

Here is the corresponding SQL statement :

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products] AS [t0]
LEFT OUTER JOIN [dbo].[Categories] AS [t1] ON [t1].[CategoryID] = [t0].[CategoryID]
WHERE [t1].[CategoryName] = 'Beverages'

Choose your camp :-)

Tuesday, July 15, 2008

C# - Learning Events

Here is my simple attempt on implementing events in C#
Any comment would be appreciate.

using System;
namespace TutorialEvents
{
    delegate void PersonHandler(string information);

    class Person
    
        protected string name = "Anonymous";
        public static event PersonHandler PersonEvt;

        public void Process(string name)
        {
            OnPersonEvt(name);
        }

        protected void OnPersonEvt(string name)
        {
            string oldname;
            if (PersonEvt != null)
            {
                oldname = this.name;
                this.name = name;
                PersonEvt(String.Format("OldName : {0} - NewName : {1}", oldname, this.name));
            }
        }
    }

    class ControlNameChange
    {
        static void myPersonEvt(string message)
        {
            Console.WriteLine(message);
        }

        public void Subscribe()
        {
            Person.PersonEvt += new PersonHandler(myPersonEvt);
        }
    }

    class Test
    {
        public static void Main(String[] args)
        {
            Person p = new Person();
            ControlNameChange controlNameChange = new ControlNameChange();
            controlNameChange.Subscribe();
            p.Process("Henri");
            p.Process("Marcel");
            Console.ReadLine();
        }
    }
}

Tuesday, July 8, 2008

Data Access Tutorial in ASP.NET 2.0

quote from site :

Series of tutorials that will explore techniques for implementing these common data access patterns in ASP.NET 2.0. These tutorials are geared to be concise and provide step-by-step instructions with plenty of screen shots to walk you through the process visually. Each tutorial is available in Visual Basic and Visual C# versions and includes a download of the complete code used.

http://www.asp.net/learn/data-access/?lang=cs#intro

Thursday, May 29, 2008

Proccesing - Sparkling Stars

Processing is a wonderful environment for those who like to experiment
with design and graphics and sound
It has been created by artists for artists.

float posx = 250;
float posy = 250;
int numstars = 36;

Star[] p = new Star[numstars];

class Star {
  float x;
  float y;
  Star(float _x, float _y) {
    x = _x;
    y = _y;        
  }

  void moveTo(float _x,float _y){
    x = _x;
    y=_y;
    for (float i=0; i<360 ;i+=4){
      stroke(random(255)+245,random(255)+245,random(12)+245,random(30));
      line(x,y,random(40)*cos(i)+x,random(40)*sin(i)+y);      
    }  
  }
}

void setup(){
  size(800,800);
  smooth();
  noStroke();
  for (int i=0; i<numstars; i++){
    p[i] = new Star(100,100);
  }
}

void draw(){
  background(0,0,30);
  for (int i=0; i<numstars; i++){
    posx = random(width);
    posy=random(height);  
    p[i].moveTo(posx,posy);
  }
  
}