Blog

posts tagged with csharp

Silverlight 3.0 Beta

0 Comments
By Fons Sonnemans, 20-mrt-2009

The Silverlight 3.0 Beta is out. Tim Heuer has written a great blog with all the new features. I have added the new SaveFileDialog to my Sudoku.

Now I can start to write a new Silverlight 3 training!.

Update 20 july 2009: Implemented Silverlight 3.0 RTW and moved the game to http://www.silverarcade.com/games/sonnemaf/sudoku 

READ MORE

C# LINQ Pivot() Function

3 Comments
By Fons Sonnemans, 22-jan-2009

I have created a LINQ extension method which can be used to Pivot an enumeration. Just like you can PIVOT a SQL Query in SQL Server.

The following example uses the Pivot() function to group Employees by Department and Function and aggregates the Salary using a SUM. The second example uses a different grouping and Counts the number of employees per group.

  class Program {

    internalclass Employee {
        publicstring Name { get; set; }
        publicstring Department { get; set; }
        publicstring Function { get; set; }
        publicdecimal Salary { get; set; }
    }

    staticvoid Main(string[] args) {

        var l = new List<Employee>() {
            new Employee() { Name = "Fons", Department = "R&D", 
                Function = "Trainer", Salary = 2000 },
            new Employee() { Name = "Jim", Department = "R&D", 
                Function = "Trainer", Salary = 3000 },
            new Employee() { Name = "Ellen", Department = "Dev", 
                Function = "Developer", Salary = 4000 },
            new Employee() { Name = "Mike", Department = "Dev", 
                Function = "Consultant", Salary = 5000 },
            new Employee() { Name = "Jack", Department = "R&D", 
                Function = "Developer", Salary = 6000 },
            new Employee() { Name = "Demy", Department = "Dev", 
                Function = "Consultant", Salary = 2000 }};

        var result1 = l.Pivot(emp => emp.Department, 
            emp2 => emp2.Function, lst => lst.Sum(emp => emp.Salary));

        foreach (var row in result1) {
            Console.WriteLine(row.Key);
            foreach (var column in row.Value) {
                Console.WriteLine("  " + column.Key + 
                    "\t" + column.Value);

            }
        }

        Console.WriteLine("----");

        var result2 = l.Pivot(emp => emp.Function, 
            emp2 => emp2.Department, lst => lst.Count());

        foreach (var row in result2) {
            Console.WriteLine(row.Key);
            foreach (var column in row.Value) {
                Console.WriteLine("  " + column.Key + 
                    "\t" + column.Value);

            }
        }
        Console.WriteLine("----");
    }
}

The results in:

Tags: CSharp

READ MORE

Silverlight 2.0 Cross Domain Policy File Checker

0 Comments
By Fons Sonnemans, 18-dec-2008

In order to make Cross Domain calls in Silverlight 2 applications, the server you wish to download data from must have a Policy File in place that allows access to your site. I have created a Silverlight Application in which you can check the content of the Policy Files (if exists).

Click on the image below to try the application

Try application

READ MORE

Silverlight 2.0 Locator for Objectmap

0 Comments
By Fons Sonnemans, 10-nov-2008

I have created a new Silverlight 2 Control which is used in the Objectmap website. This control makes it easy for the customers of accounts to find their nearest object (branch, offices or sales outlet). They can also immediately retrieve dynamic directions to this location or link to a more info page.

Have a look at this demopage, sorry in Dutch only.

This control uses: Cross Domain WCF networking, Data Binding, Isolated Storage Settings, Google Maps API calls, Cross Domain HTML Bridge and Styling.

READ MORE

Hello World in Silverlight 2 using Visual Studio 2008 SP1 Video

0 Comments
By Fons Sonnemans, 09-okt-2008

Dit is mijn eerste Silverlight 2 instructie video. In deze video toon ik hoe je een eenvoudige Hello World applicatie kunt opzetten met behulp van Silverlight 2 (RC0) en Visual Studio 2008 met Service Pack 1.

Toon Video

Download de HelloWorld.zip sourcecode. Zorg er wel voor dat je alle benodigde software geinstalleerd hebt.

READ MORE

Silverlight 2.0 RollMenu

0 Comments
By Fons Sonnemans, 24-jun-2008

I have created a RollMenu control in Silverlight 2 Beta 2. It uses an Xml file to configure the menu items.

Click on following image for a live demo.

Live Demo

READ MORE

Hot Technologies Day

0 Comments
By Fons Sonnemans, 18-jun-2008

On June 17, I did two sessions on the Hot Technologies Day for Achmea (Interpolis) in Tilburg. I have given many trainings (C#, ASP.NET 2.0, AJAX, SQL2005, VS2008) at Achmea the last year. It is a very innovative company and very fun to work for.

This Hot Technologies Day was a mini seminar with 4 sessions: Silverlight 2, VSTS DB Pro, LINQ + Entity Framework, Microsoft Office SharePoint Server. Here is a picture of me taken during the Silverlight 2 session:

In the Silverlight 2 session I gave an overview of Silverlight 2 Beta 2. You can download the slide deck here.

In the LINQ + Entity Framework session I gave an overview of C# 3.0, LINQ, LINQ to SQL and Entity Framework. You can download the slide deck here.

READ MORE

Generic BidirectionalAssociationCollection

0 Comments
By Fons Sonnemans, 25-mrt-2008

Bidirectional associations are easy to design but difficult to program. You must write lot's of synchronization code with the risk of making mistakes. Have a look at the following example in which there is a bidirectional association between Employee (Employer) and Company (Employees).

The synchronization code is implemented in the AddEmployee(), RemoveEmployee() and the (Set)Employer methods.

  public
  class Employee {

    publicstring Name { get; set; }
    publicdecimal Salary { get; set; }

    private Company _employer;

    public Company Employer {
        get {
            return _employer;
        }
        set {
            if (Employer != value) {
                
                // Remove from 'old' Employerif (Employer != null) {
                    Company old = _employer;
                    this._employer = null;
                    old.RemoveEmployee(this);
                }

                // Set the Employer    this._employer = value;
                
                // Add to 'new' Employerif (Employer != null) {
                    Employer.AddEmployee(this);
                }
            }
        }
    }
}

publicclass Company {

    publicstring Name { get; set; }

    privatereadonly List<Employee> _employees = new List<Employee>();

    publicvoid AddEmployee(Employee emp) {
        if (!_employees.Contains(emp)) {
            _employees.Add(emp);
            emp.Employer = this;  // Synchronize
        }
    }

    publicvoid RemoveEmployee(Employee emp) {
        if (_employees.Contains(emp)) {
            _employees.Remove(emp);
            emp.Employer = null; // Synchronize
        }
    }
}
Tags: CSharp

READ MORE

ExtensionMethod.net

0 Comments
By Fons Sonnemans, 09-dec-2007

Together with my friend Loek van den Ouweland I have created our new website: ExtensionMethod.net. This website is setup for finding and sharing .NET 3.5 ExtensionMethods written in C# 3.0 or VB.NET 2008.

I hope you will use it to publish your own extension methods. It is still in BETA but we hope to go live real soon.

Tags: CSharp

READ MORE

Trace LINQ to SQL Queries

0 Comments
By Fons Sonnemans, 04-dec-2007

ASP.NET Tracing is a powerful mechanism. LINQ to SQL doesn't support it out of the box. The DataContext class however has a Log property which can be set to a TextWriter object. This TextWriter object writes all SQL queries which are executed.

I have created a small class which derives from TextWriter to write all SQL queries to the System.Diagnostics.Trace class. I found out that I only had to override the WriteLine(string) and Write(string) methods.

The easiest way to assign a new TextToTraceWriter to the Log property of the DataContext object is by implementing the partial OnCreated() method in a partial class. See the following example in which also the Trace Category is set.

Tags: CSharp

READ MORE

All postings/content on this blog are provided "AS IS" with no warranties, and confer no rights. All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer or sponsors. The content on this site is licensed under a Creative Commons Attribution By license.