Archives for : September2008

Generics, Params, IEnumerables (well, yield), Oh my!

Hello all,

Well, what once was a simple weekly email about cool stuff I ran across while working in DotNet has suddenly become very complicated. I spoke briefly with the devs on Generics the other day and wanted to throw together a more concrete example of using them. In the attached solution you will find a quite thorough example of using generics in a BinaryTree class I wrote. It’s important to remember that T is simply a type and tells your code what type to expect. Please ignore the bit of kludge in the AddNode method between IComparable and IComparable<T>. This was meant to be thrown together and I didn’t want to spend too much time on it.

In case you missed it above, here is the attached solution

In looking at the code for Form1 you can see the generics in action in the button1_Click event. The power of generics really comes through for compile time issues with not having to worry about casting. Here we create trees for int, string and a class I added called City. Since City implements IComparable we can use it for the tree.

By declaring,

BinaryTree<int> intTree = new BinaryTree<int>();

we know we have a btree that has ints and without casting we can do with
those ints as we please. Then any methods in the btree knows that the
value is an int.

This btree example, however, has two other things that are pretty cool.
The first is the use of an add method that takes
params T[] Items

I love using params as it allows for variable length parameters on
methods. Um, well, not a lot to say on params, just that they’re cool.

The other cool thing is the usage of yield in the ScanInOrder method.
yield basically allows you to return from a method when iterating over a
list of items where the code will remember where you were at so that you
can return back to that position. The cool thing about using yield is
that, as shown in the sample, you can use yield multiple times in the
same method. As we all know for an InOrder traversal of a binary tree
we basically move down the left side of the tree, give our value and
then start working on the right side of the tree (well, y’all know its a
bit more then that but I don’t think I have to explain it). Here in the
ScanInOrder method of the BinaryTree class it recursively calls
ScanInOrder on the left nodes of the tree items iterating in a foreach
loop yielding each of the results. Then it returns it’s own result and
finally recursively iterates in a foreach loop over the nodes on the
right side of the tree yielding each of those results.

In the morning my son Edison and I watch Curious George together on the
couch. Right before the cartoon starts KUAT does this cutesy animation
with a song in the back ground that says, “Learn something new
everyday. You learn a lot that way.” Hopefully I’ve helped with that
goal, 🙂

Brian

?? Is the way of the WORLD, HAHAHA

 Recently I gave a quick talk to the devs at the court house about the
coalesce operator. So why do you care? Well, you probably don’t so
just quit reading right now.

For those of you that are still around the coalesce operator works like
COALESCE in T-SQL.

For instance:

SELECT COALESCE(
hourly_wage * 40 * 52,
salary,
commission * num_sales) AS 'Total Salary' FROM wages

Here in T-SQL we can see a simple select statement that will determine a
person’s total salary regardless of if they are on wage, salaried or on
commission since an employee on wage would have a null salary and null
commission but an employee on salary would have a null wage and null
commission.

Basically COALESCE uses the first value it finds that doesn’t result in
a null.

So how can this help you?

Does this look familiar? (Um, I don’t really care if it looks familiar,
just keep reading. You’ve come this far.)

//set cust first name
if(cust.FirstName != null)
txtFirstName.Text = cust.Firstname;
else
txtFirstName.Text = "";

Well, with the ?? (a.k.a. coalesce operator) you can trim this down to
txtFirstName.Text = cust.FirstName ?? “”;

In T-SQL you can put an unlimited number of commas in the function to
come up with a bunch of values to check. With the ?? operator you can
just string them along, i.e.:

txtFirstName.Text = cust.FirstName ?? cust.LastName ?? cust.Nickname ?? "Name not found";

This can be taken a step further since ?? can be used with primitives.
This makes it very nice when dealing with nullable primitives.
Assuming MetaData has an Occurrence that is a int? you can get the
occurrence like:

int occurrence = MetaData.Occurrence ?? 0;

No casting just nice code.
Well, later ‘yall!

Brian

Namespace Aliasing

 When working with WPF, though this is applicable universally, a lot of
times you end up working with both the old WinForms stuff and the new
WPF stuff.  The object names between the two namespaces are almost
identical which can be a real pain meaning anytime you reference “Label”
in code (but outside the XAML) you have to use the fully-qualified
namespace.  Well, you can short cut (alias) that by doing the following:

// WPF namespace
using WPF = System.Windows.Controls;
// WinForms namespace
using WinForms = System.Windows.Forms;

Huh, an equals in the namespace?  Yes, and now rather then having to use
System.Windows.Controls.Label for a WPF label or
System.Windows.Forms.Label for a WinForms label you can just use WPF.Label
for a wpf label and WinForms.Label for a WinForms label.  That should
save you some typing!

be ^ !be

 Unlike the & and | logical operators which seem to have dubious use the
^ (XOR, exclusive or) logical operator can make things quite a bit easier.

For instance ever write a IComparable?

rather then:

if (o1 == null && o2 != null)
    return false;
if (o1 != null && o2 == null)
   return false;

you can just do:
if(o1 == null ^ o2 == null)
   return false;

like the other logical operators both sides of the conditional must be
evaluated but since we’re doing exclusive or-ing here it makes sense to
do it rather then in the cases of the other logical operators where fail
fast (or short-circuiting) conditional operators (like && and ||) would
work better.

ASP.NET Wiki

UPDATE 07/23/14:

I’ve been getting a few hits on this link so I wanted to put in a note that Microsoft no longer maintains the below site and now refers it to the CodeProject site.  I’ll maintain this post for archival purposes but the link is now dead.

Thanks,
Brian

 

http://wiki.asp.net/

from Microsoft, good starting point for any asp.net questions. Lots of
links to blogs and videos for just about every topic.

Stop that build!

 I’m sure we’ve all clicked build by accident, especially with a dnn project.

Did you know you can stop it?

Ctrl + Break

ref:stevenharman.net