Welcome to WindowsClient.net | Sign in | Join

BestSnowman's WPF Blog

Sponsors





  • advertise here

August 2008 - Posts

XamlPadX

I always thought the original XamlPad was a cool tool, at the time I was using it WPF was still in beta and Visual Studio 2005 didn't really have designer support. I lost track of the tool sometime after .NET 3.0 was released, and honestly I don't know if its still available if you download the Windows SDK. All I know is that a full install of Visual Studio 2008 Team System Edition does not appear to have it. Well it turns out someone else has created a similar tool (named XamlPadX) and has even made it to version 4 without me noticing (XamlPadX 4.0).

IsTrue ? true : false

This is a repost from my old blog and one of my favorite recent posts so I thought I would repost it.

Today I was looking through some code, that I didn't write, and found an interesting nugget. For those of you who haven't figured out the title yet let me explain that first. In C# the ?: operator is pretty simple and handy. Lets look at this example:

cond-expr ? expr1 : expr2

If cond-expr evaluates to true then expr1 is returned, and if it evaluates to false then expr2 is returned. Simple enough right?

   1:  bool SomeValue = true;
   2:  string message = SomeValue ? "Yes" : "No";
   3:  // Message is "Yes" 
   4:  message = !SomeValue ? "Yes" : "No";
   5:  // Message is "No"

The example I found was a little bit different, the example I found expr1 was true and expr2 was false.

   1:  bool IsSame = item1 == item2 ? true : false;

So basically if (item1 == item2) evaluates to true then IsSame is set to true and if false than IsSame is naturally set to false. This may seem to be fine at a glance, but it is the same as this:

   1:  bool IsSame;
   2:  if(item1 == item2)
   3:  {
   4:       IsSame = true;
   5:  }
   6:  else
   7:  {
   8:       IsSame = false;
   9:  }

There are really two problems with this, first its a good example of The Department of Redundancy Department. The second problem is that it confuses the intention of the line of code. If you have this:

   1:  bool IsSame = item1 == item2;
   2:   
   3:  // or in a slightly more explicit way
   4:  bool IsSame = (item1 == item2);

which is logically the same it is clear that you want IsSame to be true when item1 and item2 are are equal at first glance. I spent a little while trying to figure out why this is needed. The only reason you would need to use true and false as expr1 and expr2 is if bool had more than two values. I guess maybe its future proofing for maybe being added to the possible boolean values.

-Matt Newman

Posted: Aug 26 2008, 08:44 PM by mattnewman |
Filed under:
About Me

I would like to start this blog with an introduction of myself, my name is Matt Newman and by day I'm an ASP.NET web developer. By night I do a variety of hobby and self improvement programming, mostly windows applications with WPF. I do some ASP.NET (MVC is cool!) but prefer to spend my free time not worrying about stateless connections. I'm actively working on a couple of WPF projects including a creatively named Product Key Manager, which is available free on CodePlex, and a couple other that I'm not making public at this time. I'm also presenting on Windows Presentation Foundation at the next Mankato .NET User Group September 23rd (more details to come).

I'm not an expert but someone who loves WPF and .NET programming and will be posting mostly on my adventures as such. Sometimes I post off topic stuff like how Adobe Acrobat Reader's update application annoys me to no end and occasionally non technical stuff about my life. If you are a new reader I hope you enjoy reading and commenting and if you are a reader of my last blog I'm glad you made it over.

-Matt Newman

Posted: Aug 26 2008, 08:28 PM by mattnewman
Filed under:
Page view counter