Using XAML for Custom Object Graphs

XAML is best known for its use with WPF/Silverlight, but the XAML parser is also a powerful generic serialize/deserialize tool for many .NET object graphs.  For example, XAML is used for Windows Workflow (WF).  This post will show a basic example and some tips to use XAML parsing in your own code. Tips to Watch Out For If you put a .xaml file in your VS project, be sure to set it’s "Build" property to "None" (from the default of "Page").  If you don’t VS will cause a build error on the XAML line where your assembly is defined. … Continue reading Using XAML for Custom Object Graphs

SerialPort (RS-232 Serial COM Port) in C# .NET

.NET has a great class called SerialPort (MSDN reference) part of .NET 2.0 and is freely available in C# Express on MSDN. It is easy to use.  Here’s how… BTW, this article is about communicating through the PC’s Serial COM RS-232 port using Microsoft .NET 2.0 or later by using the System.IO.Ports.SerialPort class. If you’re looking for USB info, see here.   Example Application & Sample Code: SerialPort Terminal Prerequisites: You will need Microsoft .NET 3.5 to run the app.  It is installed as part of the regular Windows Updates (make sure your computer is fully up to date, see … Continue reading SerialPort (RS-232 Serial COM Port) in C# .NET

Reverse Enum Lookup

Sometimes you may have the string name of an enumeration identifier, and want its enum type. For example, you write a enum identifier as a string to a file and want to read it back and obtain the original enumeration type. This takes place when you serialize a class to an XML file and deserialize it back using XmlSerializer. private enum Direction {North, South, East, West}; private Direction ParseDirection(string name) { return (Direction) Enum.Parse(typeof(Direction), name, true); }

Obtaining Indented XML as a String

Using XmlDocument.Save(string file) produces a file with nicely indented elements. XmlDocument.OuterXml returns a string without any formatting. If you want a nicely formatted string (to display to the user, write to console, etc), without directly writing to a file, you must use the XmlTextWriter. The using statements in the method makes sure the resorces are disposed of sooner than later. Here is the code to return a string of formatted XML from an XmlDocument. Download Example Code (requires Visual Studio 2005) using System.IO; using System.Xml; public static string FormatXML(XmlDocument doc) { // Create a stream buffer that can be read … Continue reading Obtaining Indented XML as a String

Creating Quick Tests in C#

Update on 5/2/07:  This post has nothing to do with Unit Testing (it was written before I was aware of unit testing) but has to do with write little bits of code to try new stuff out.  It is also only for VS03.  For the current version (VS05), see this post: CoadNet VS Project Templates, Quick Code + Console App for C# in VS05 CoadTools Quick Test C# Project Template (setup .msi) for Visual Studio .NET 2003 IntroductionVery often I need to test a tidbit of code, do some research on a class, etc. In fact, I was creating several … Continue reading Creating Quick Tests in C#

Property Event Pattern

IntroductionSince the advent of .NET, one of my most frequent and useful patterns is using an event to notify change in state. Applying this pattern has reduced overall code, improved reliability, and made maintenance and expansion easier. Why? The ContextWithout events, spaghetti code occurs real quickly when one method changes state that other code is dependant on without notifying that the change is made. This is one of the most frequent causes of coding errors (bugs) in mid-to-large applications that I have seen. Events go a long way in solve this issue. This should be obvious with public properties that … Continue reading Property Event Pattern

Associating a File Extension and Context Menus with an Application in the Registry

You can easily assign a file extension to open with your application in the registry. Verbal DescriptionThe extension is added as a key under “HKEY_CLASSES_ROOT” with the (default) value set to the file’s type (a string arbitrarily assigned by yourself). Then you add a key with the file’s type also to “HKEY_CLASSES_ROOT” with its (default) value set to the file’s type description (that is shown in Exporer). Under the same key, add they keys “shell/open/command” with the (default) value being the program to execute. Use “%1” with the quotes to pass the file’s name to your app. You can also … Continue reading Associating a File Extension and Context Menus with an Application in the Registry

Programming Practices Policy v3.0 Released

Now available is a new version of the synopsis of C# coding practices that are being used by the majority of MVPs, professional C# coders, and my associates. These practices are being used by the development community and this is an abbreviation that I have assembled for colleagues. Programming Practice Policy.pdf v3.0

C# Team FAQ Blog

The C# team at Microsoft has started a Frequently Asked Questions (FAQ) blog where they answer some interesting topics.  Definitely a good read for C# developers.  I’ve found it interesting to see the reasoning behind some of the descriptions made in the C# language.

Two New CoadTools, AdvancedFileProperties & OrderedStringDictionary

My protege, Radoslav (Radi) Ivanov (age 16), has published his first code utilitity!  It is an “Advanced File Properties“ reader that retrieves the NTFS extended properties of a file.  It provides a DataTable wrapped method for an included COM DLL. I created an “Ordered String Dictionary“ that implements an ArrayList to create a string key/value pair dictionary that keeps the ordering of its elements.  The System.Collections.Specialized StringDictionary uses a HashTable that looses the order of its pairs and NameValueCollection does not support insertion at a specific location.  This class is in the “SpecializedCollections“ CoadTool. Free CoadTools