Wednesday, October 29, 2008

[VS2008] Free Code Metrics Plugin

A few days ago, I was looking around to see if I could find out how to run some basic code metric analysis on my projects. It seems like a few editions of VS2008 [the IDE we're using at work] DOES have some built in code metric evaluation, but only in specific versions of the software. We have a the Professional Edition not the Team Edition... so no go.

But! I found a free VS plugin [thanks to the fine folks at Exact Magic Software] that will add similar functionality!

http://www.exactmagic.com/products/studiotools/index.html

Now, currently the numbers don't mean a whole lot to me since I have nothing to compare them with, but I'm going to start tracking them just to see :)


.

Tuesday, October 28, 2008

[Silverlight] Toolkit Released!

Neat, a new Silverlight toolkit has been released by the MS devs which will add some more components for developers to use and give feedback on.


http://www.codeplex.com/Silverlight

The Silverlight Toolkit is a collection of Silverlight controls, components and utilities made available outside the normal Silverlight release cycle. It adds new functionality quickly for designers and developers, and provides the community an efficient way to help shape product development by contributing ideas and bug reports. This first release includes full source code, unit tests, samples and documentation for 12 new controls covering charting, styling, layout, and user input.



.

Friday, October 24, 2008

[C#] "var" ... Implicitly Typed Variables - When to use?

Alrighty, so during a code review it was brought to my attention that as of C# 3.0 programmers can now use implicit declaration of variables, which was completely contrary to my previous understanding of the language - in that I thought it was solely strongly typed much like C/C++. That is not the case.
Explicit Declaration: int myCount;
Implicit Declaration: var myCount;
Implicit declarations rely on the compiler to chose the best type for you. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.

Now while I can see this being handy for many reasons [from what I've read LINQ requires this type of functionality - but I haven't used LINQ yet]... it also troubles me - mostly due to readibility issues if overused. I've done some quick searches and I am not alone in my concerns. I'm just figured I'd list here when *I* think that the use of var can and should be applied for inferring a type.

*** Implicit Usage [My Thoughts] ***
Case 1: If it's required.
Duh... such as with LINQ examples... sometimes you just can't get around it ^^

Case 2: If you are dealing with a very, very, very long type name.
Example:
OhHaiThisIsMyLongClassName myLongClass;
Would become:
var myLongClass;

Case 3: If you Initialize the object on the same line.
Canvas myObj = new Canvas();
Would become:
var myObj = new Canvas(); // <-- We know in the same line what this variable's type is.

Now granted... if the name of your variable does not indicate what it is actually representing, then perhaps there is a programming issue with the naming convention used. I've seen this as an arguement in favor of using "var" - it forces descriptive naming conventions which would in the end actually increase the readibility of a programming.

Really in the end I believe it comes down to personal style - but it's good to keep in mind the pros and cons of using both explicit/implicit typing of variables.


MSDN Documentation on Implicit Typed Variables
Another discussion about when to use "var"

.

Tuesday, October 21, 2008

[Silverlight, C#] Adventures in Accessing Path Data

So I've been stumped for awhile on this, and it seems I was simply misunderstanding something about the Path Syntax used to describe path data, and as I discovered shortly after, there is a limitation in what Silverlight can parse.

Let's start at the beginning. I had a path object, that I took from an example, that looked like this:

[Path Name="shape_path"
Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000"
Width="92.5" Height="57.617"
Data="M137,122 C227,161 168.5,204.5 228.5,159.5" /]

I wanted, in my code behind, to access the Data property so that I could serialize the object into string whenever I wanted to. The problem I was having was that when I tried to access the Data by casting, I was getting an empty collection back.

Essentially PathGeometry g = (PathGeometry)path.Data was giving me an empty collection [Figures] and I had no way to get access to the data. I poked around the Silverlight forums and it seemed like other people were having this issue, and this used to work pre-Beta2, so there was some confusion.

Problem 1: My Misunderstanding of the Types of Path Markup Syntax

As it turns out though, MS never intended a PathGeometry to be accessed in that manner. And here's why [and what I had overlooked]. With Path Markup Syntax, there are actually 2 different types of paths:
StreamGeometry : You use the StreamGeometry mini-language when setting a property of type Geometry, such as the Clip property of a UIElement or the Data property of a Path element. The following example uses attribute syntax to create a StreamGeometry.
[Path Stroke="Black" Fill="Gray"
Data="M 10,100 C 10,300 300,-200 300,100" /]

PathFigureCollection : You use the PathFigureCollection mini-language when setting the Figures property of a PathGeometry. The following example uses a attribute syntax to create a PathFigureCollection for a PathGeometry.
[Path Stroke="Black" Fill="Gray"]
[Path.Data]
[PathGeometry Figures="M 10,100 C 10,300 300,-200 300,100" />]
[/Path.Data]
[/Path]
As you can see from the preceding examples, the two mini-languages are very similar. It's always possible to use a PathGeometry in any situation where you could use a StreamGeometry; so which one should you use? Use a StreamGeometry when you don't need to modify the path after creating it; use a PathGeometry if you do need to modify the path.
In my case, I actually needed to access the Figures collection, so I needed to use the longer notation. In my case, my error was that I had taken an example from somewhere, but it was not the correct syntax for what I ultimately wanted to do.

Now I'm not certain why there is a difference between these two types of paths since they seem so similar, although I imagine a little deeper reading into Geometries would tell me that.

Note: The following link is to documentation for WPF, which is where part of my confusion lay... since support in WPF is different then in Silverlight.
MSDN Documentation on Path Markup Syntax
The correct link is contained in the section "A Good Explanation" at the end of this post.


Problem 2: In Silverlight, Path.Figures Cannot be Parsed using the Mini-Lanugage Syntax

So yay right?!? This should be all fine and dandy! I change my XAML to use the correct Mini-Language as indicated in Problem 1, I go to build/run my application and BAM!

"Invalid attribute value M 10,100 C 10,300 300,-200 300,100 for property Figures. [Line: 5 Position: 27]".

With a little more searching I find a post indicating that:

The PathGeometry.Figures property does not support the path "mini-language" syntax in Silverlight. You have to use the verbose explicit markup to describe those figures in your XAML.

Now I just want to scream... after all that, Silverlight can't even PARSE the mini method of syntax and I need to make sure that the path I'm getting is using the long verbose method :(

Something like this:

[PathGeometry]
[PathGeometry.Figures]
[PathFigure]
[PathFigure.Segments]
[LineSegment Point="150,0"/]
[/PathFigure.Segments]
[/PathFigure]
[/PathGeometry.Figures]
[/PathGeometry]

Blech. How irritating, especially when many examples are given on MSDN in the mini syntax, and from what I've heard the WPF XAMLWriter also outputs the mini syntax.

Anyways, just a heads up, this seems to be the way it is.

Long story short: YOU HAVE TO BE VERBOSE WITH PATHGEOMETRY



A Good Explanation: What is actually supported in Silverlight, and how it works in WPF as well...

First, my hats off to all the people in the Silverlight community that help dummies like me out with issues on forums ^^. Big thanks to Wolf Schmidt (MSFT) who gave me this excellent explanation of what is going on:
PathGeometry.Figures does not support the Path minilanguage/syntax in Silverlight. However, Path.Data DOES support the Path minilanguage/syntax in Silverlight.

In Silverlight, if you use the minilanguage and set Path.Data, you get your intended path and its various parts rendered, but you do NOT get a run-time object tree representation of how that minilanguage was parsed such that you could walk into Figures, Segments, etc. You have to use the verbose form of markup by putting various PathFigure-type object elements under PathGeometry.Figures IF you want discrete run-time "parts" (and for best results, put some "Name" attributes on there so that you don't even need to walk, you can just use FindName).

BTW, in WPF, you also do not get object-tree representation of a path created via the mini-language. The only delta is that Silverlight only supports parsing/processing of the minilanguage on Path.Data whereas WPF supports it for either Path.Data OR PathGeometry.Figures.

PS: the MSDN article you linked two posts above is specifically the WPF version of the Path Markup Syntax information. You need to look at http://msdn.microsoft.com/en-us/library/cc189041(VS.95).aspx which is the Silverlight version. In general, you do have to keep an eye on whether you are looking at a WPF topic or a Silverlight topic when you look at MSDN, because many cases the TOC organization/titling etc. is similar between WPF and Silverlight but the information on the specific pages is often quite different because of the inherent technology differences.

My Silverlight Forum Post

.

Friday, October 10, 2008

[C#] Object Creation with Property Initialization

So a colleague of mine showed me a nice simple way in C# to both create and initialize an object in one step. I thought this was really neat syntax since it both saves lines of code and also saves some execution as the object is initialized with a value instead of a default which is THEN changed.

So something like this:
Rectangle myRec = new Rectangle();
myRec.Width = 50;
myRec.Height = 50;
Can become:
Rectangle myRec = New Rectangle() { Width=50, Height=50 };


There are some restrictions though:
  • Can only initialize properties or fields accessible by the object being initialized.
  • This means that you cannot set attached properties in this manner [ie. Canvas.Top], as these properties can only be set with the SetValue function.
  • The assignments in the initializer is treated the same as assignments to members of the field/property.

See here for a little more
And this msdn page

.

Wednesday, October 1, 2008

[Silverlight] Silverlight Beta 2 RC0 Released

Applications built using this release will work off of the Silverlight 2 RC0 Developer Runtime only. This is not intended for public deployment, as end users will not have a way to install a suitable plug-in to view the application. No existing public versions (released or beta) of Silverlight 1.0 or 2 Beta 2 will be auto-upgraded to this runtime. This is only for developers.

See http://silverlight.net/GetStarted/sl2rc0.aspx
and http://weblogs.asp.net/scottgu/archive/2008/09/25/silverlight-2-release-candidate-now-available.aspx

for more including downloads and breaking changes / new features.

Problems?
After I did the update, when I ran my application in IE I was prompted with the "Install Microsoft Silverlight" button... but how could that be? I just installed the new runtime?

With a little searching I came across this:

A quick fix when updating apps to RC0...if all you get is the "Install Silverlight" button, make sure your object type in your page is changed to
application/x-silverlight-2 instead of applicatiion/x-silverlight-2-b2

And poof! Works!


.