Posts Tagged Basic

Fluent C# Review : Chapter Two

I am reviewing an advanced copy of the book Fluent C# by noted .NET author Rebecca Riordan.

My focus today will be on Chapter Two. Chapter Two gets into some of the nuts and bolts of the .NET Framework and writes the initial “Hello World” application that these beginner books almost always do. They begin by putting up a very nice illustration of how .NET works. Truly I have not seen it explained in such a way before!


They give you a little quiz on what you picked up.

Then there is an explanation of why the .NET Framework is a better way to go than say a Visual Basic 6 way of thinking. I am not sure that to a beginner programmer they understand or need to know any of this. But they certainly make good points. Being an old VB6 and VBA programmer myself I certainly could appreciate what was said.

Then they proceed into building the first application. But there is a little twist. Riordan chooses to not even bother with winforms and instead chooses to plow right into a Windows Presentation Foundation (WPF) application. At first the choice seemed odd to me. But as I considered it this makes sense. WPF is the future. Winforms is dying. It’s a sad truth but it is what it is.

Riordan walks you through the process of creating a WPF application in Visual Studio 2010 while beautifully illustrating how to do that task.


Then the first example of modifying a property in the WPF gui is introduced. Really it is quite well done. The appropriate message box appears saying “hello world!” is displayed. She tells the user to take a break and congratulates them on their first application. She then displays and explains the code editor in the same easy going manner. Another note that often goes on through out this book she often gives the novice programmer fun little facts to digest, so as to have fun while learning. Imagine that! C# learning being made fun!

We will be covering Chapter Three in the next entry where the Visual Studio interface will be reviewed in more detail.


, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

1 Comment

Write DataTable to an MS Word Table Efficiently with C# using a Dynamic Type

This is a C# adaptation of the code I wrote to write a datatable to a Microsoft Word document table for . But that doesn’t really begin to tell the story here. In we have been accustomed to being allowed to leave parameters empty when automating the creation of a table in Microsoft Word. C# has not permitted me that luxury which to be honest is probably a better code practice. So prepare to meet the Type.Missing object! In addition the default item that we learned to love/hate in Visual Basic for Applications (VBA) code years ago also is not used in C#. Finally in declaring the range object for the table it became an opportunity to use the new dynamic reference type keyword, which was designed for such situations. Check out this video on the subject which is quite excellent.

For these reasons you will see key differences between the two sets of code. Don’t forget to import Microsoft Word as a COM reference and do your import statements. As always feel free to comment or email. Have a great day!

using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;

public void CreateWordTableWithDataTable(DataTable dt)
            int RowCount = dt.Rows.Count; int ColumnCount = dt.Columns.Count;
            Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
            //int RowCount = 0; int ColumnCount = 0;
            int r = 0;
            for (int c = 0; c <= ColumnCount – 1; c++)
                DataArray[r, c] = dt.Columns[c].ColumnName;
                for (r = 0; r <= RowCount – 1; r++)
                    DataArray[r, c] = dt.Rows[r][c];
                } //end row loop
            } //end column loop

            Word.Document oDoc = new Word.Document();
            oDoc.Application.Visible = true;
            oDoc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
            dynamic oRange = oDoc.Content.Application.Selection.Range;
            String oTemp = “”;
            for (r = 0; r <= RowCount – 1; r++)
                for (int c = 0; c <= ColumnCount – 1; c++)
                    oTemp = oTemp + DataArray[r, c] + “\t”;

oRange.Text = oTemp;
object Separator = Word.WdTableFieldSeparator.wdSeparateByTabs;
object Format = Word.WdTableFormat.wdTableFormatWeb1;
object ApplyBorders = true;
object AutoFit = true;

object AutoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitContent;
            oRange.ConvertToTable(ref Separator,
        ref RowCount, ref ColumnCount, Type.Missing, ref Format,
        ref ApplyBorders, Type.Missing, Type.Missing, Type.Missing,
         Type.Missing, Type.Missing, Type.Missing,
         Type.Missing, ref AutoFit, ref AutoFitBehavior,
            oDoc.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
            oDoc.Application.Selection.Tables[1].Rows.Alignment = 0;

            //gotta do the header row manually
            for (int c = 0; c <= ColumnCount – 1; c++)
               oDoc.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = dt.Columns[c].ColumnName;

            oDoc.Application.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;


, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a comment