August, 2009

  • Wriju's BLOG

    Technology Presentations for us

    • 0 Comments

    Watch it out online at http://www.microsoft.com/emea/msdn/spotlight/

    DevDays 2008 'Bring your Ideas to Life'Geek Night Netherlands 2008MIX Essentials 2008 NetherlandsAcademic DayHeroes Happen Here - US Premier Launch DallasTechEd Developers 2007Microsoft 2007 SOA and Business Process ConferenceDevDays 2007REMIX 2007Windows Vista and IE readiness tourTechED: DevelopersWinFX RotterdamExperience the Difference Tour

    Namoskar!!!

  • Wriju's BLOG

    C# 4.0: Improved COM Interop

    • 0 Comments

    The question often arises what is so special in C# 4.0 about COM Interop. To me it is a lot. I will try to explain why..

     

    Assume you are trying to get the list of proccess running locally in your machine and creating them using Excel chart and copying that chart to Word.

     

    I have given the working code at the end of this article. Here I want to highlight some features of C# 4.0 which helps us to write better code,

     

    Named Parameter

    ============

    In the code at

    chart.ChartWizard(range.CurrentRegion,               

            _missingValue,

            _missingValue,

            _missingValue,

            _missingValue,

            _missingValue,

            _missingValue,

            "Memory Usage in " + Environment.MachineName,

            _missingValue,

            _missingValue);

     

    There are some unnecessay code which can be removed as below,

     

    chart.ChartWizard(Source: range.CurrentRegion,

        Title: "Memory Usage in " + Environment.MachineName);

     

    Like that you can get rid of many such codes

     

    Improved COM Interop

    ===============

    Now another cool thing about COM Interop Improvement is, no need to pass ref _missingValue.

     

    So the code

    word.Documents.Add(ref _missingValue, ref _missingValue,

        ref _missingValue, ref _missingValue);

     

    can be written as

    word.Documents.Add();

     

    Dynamic Language Runtime

    ===================

    Another noticable part is any method’s tool tip shows like,

    void ChartWizard(dynamic Source = null, dynamic Gallery = null, dynamic Format = null, dynamic PlotBy = null, dynamic CategoryLabels = null, dynamic SeriesLabels = null, dynamic HasLegend = null, dynamic Title = null, dynamic CategoryTitle = null, dynamic ValueTitle = null, dynamic ExtraTitle = null);

     

    So it fully dynamic.

     

    So the below code would work fine,

     

    using Excel = Microsoft.Office.Interop.Excel;

    using Word = Microsoft.Office.Interop.Word;

     

    static object _missingValue = Missing.Value;

    static void Main(string[] args)

    {           

        GenerateChart(copyToWord: true);

    } 

     

    static void GenerateChart(bool copyToWord = false)

    {

        var excel = new Microsoft.Office.Interop.Excel.Application();

        excel.Visible = true;

        excel.Workbooks.Add(_missingValue);

     

        excel.get_Range("A1").Value2 = "Process Name";

        excel.get_Range("B1").Value2 = "Memory Usage";

     

        var processes = (from p in Process.GetProcesses()

                        orderby p.WorkingSet64 descending

                        select p).Take(10);

     

        int i = 2;

     

        foreach (var p in processes)

        {

            excel.get_Range("A" + i).Value2 = p.ProcessName;

            excel.get_Range("B" + i).Value2 = p.WorkingSet64;

     

            i += 1;

        }

     

        Excel.Range range = excel.get_Range("A1");

        Excel.Chart chart = (Excel.Chart)excel.ActiveWorkbook.

            Charts.Add(

                _missingValue,

                excel.ActiveSheet,

                _missingValue,

                _missingValue);

     

        chart.ChartWizard(range.CurrentRegion,               

            _missingValue,

            _missingValue,

            _missingValue,

            _missingValue,

            _missingValue,

            _missingValue,

            "Memory Usage in " + Environment.MachineName,

            _missingValue,

            _missingValue);

     

     

        chart.ChartStyle = 45;

        chart.CopyPicture(Excel.XlPictureAppearance.xlScreen,

            Excel.XlCopyPictureFormat.xlBitmap,

            Excel.XlPictureAppearance.xlScreen);

     

        //Copy the chart to Word

        if (copyToWord)

        {

            var word = new Word.Application();

            word.Visible = true;

     

            word.Documents.Add(ref _missingValue,

                ref _missingValue,

                ref _missingValue,

                ref _missingValue);

     

            word.Selection.Paste();

        }

    }

     

    Namoskar!!!

  • Wriju's BLOG

    C# 4.0 and Parallel Programming Resources

    • 0 Comments

    C# 4.0

     

    The Future of C# by Anders Hejlsberg at PDC 2008

    http://channel9.msdn.com/pdc2008/TL16/

     

    Slide Deck

    http://mschnlnine.vo.llnwd.net/d1/pdc08/PPTX/TL16.pptx

     

    Sample Code and Whitepapers

    http://code.msdn.microsoft.com/csharpfuture/

     

    C# 4.0, Dynamic Programming and JSON

    http://www.nikhilk.net/CSharp-Dynamic-Programming-JSON.aspx

     

    Easy to understand Co- and Contra Varinace

    http://blogs.msdn.com/charlie/archive/2008/10/28/linq-farm-covariance-and-contravariance-in-visual-studio-2010.aspx

     

    Parallel Programming

     

    PDC 2008 Session by Daniel Moth

     

    Parallel Programming for Managed Developers with the Next Version of Microsoft Visual Studio

    http://channel9.msdn.com/pdc2008/TL26/

     

    Demo Samples & Slide Decks

    http://www.danielmoth.com/Blog/2009/07/updated-beta1-slides-for-parallelism.html

     

    Walkthrough: Debugging a Parallel Application

    http://msdn.microsoft.com/en-us/library/dd554943(VS.100).aspx

     

    MSDN Home Page

    http://msdn.microsoft.com/concurrency

     

    Team Blog

    http://blogs.msdn.com/pfxteam/

     

    Namoskar!!!

     

Page 1 of 1 (3 items)