Simple WPF PasswordBox Answer

If you use a PasswordBox in WPF, the property you need to use to read the password typed in by the user is called “Password.”

Obviously the developer(s) of this control thought this was a perfectly cromulent and obvious name for the value that contains the password. Maybe it’s just me, but it wasn’t obvious at all. This is mostly because every single other control uses either .Text or .Content to hold their content. You would naturally expect the password control to follow a similar convention (there are two, pick one). Unfortunately, instead it uses a property unique to this control to handle this data in a manner that no other password control does.

Maybe I am the only person who has had a problem figuring out how to get the value from this contorl. In googling for the answer I didn’t find it. I downloaded the sample from MSDN and, believe it or not, in the entire sample they demonstrate many ways to use the control except, well, reading a value from it.

The only way I found the answer is that I have the book Windows Presentation Foundation Unleashed by Adam Nathan.

The end

BitArray Annoyance

The BitArrayclass in .Net is used to store a set of true and false values. It really is simply an array of Boolean values. And if Microsoft had left it there … just a specialized collection of bools, no one would have said anything. They could have called it BoolArray and everyone would have gone home happy. Instead, however, they chose to name the class BitArray and provide methods on that class to do logical operations on the array of Booleans. They added AND, OR, NOT, and XOR. So great! Now I can do bitwise operations on my array of Booleans.

But what I can’t do is transform that BitArray into an integer value. Arguably, there are good reasons for this. The BitArray class is designed to hold very large numbers of bits. It can hold more bits than can fit into any single data type. So they couldn’t simply provide a ToInt32() or ToInt64() method and be done with it. No single data type could hold all the values of a BitArray.

But there is an answer to this problem. In fact, Microsoft had already found that answer for the BitArray. If you look at the constructor of the BitArray you will see that it can take an array of integers. MSDN has this to say about the constructor:

The number in the first values array element represents bits 0 through 31, the second number in the array represents bits 32 through 63, and so on. The Least Significant Bit of each integer represents the lowest index value: “ values [0] & 1” represents bit 0, “ values [0] & 2” represents bit 1, “ values [0] & 4” represents bit 2, and so on.

So the solution should be obvious. A method is needed to return an array of integers. The BitArray class needs a ToInt32Array method that works in the same way as the constructor does. This way you can get back an array of integers just like you put into it.

Here is my version of such a function:

        int[] ToInt32Array(BitArray ba)
            int[] values = new int[ba.Length/32];
            Byte[] bytes = new byte[ba.Length];
            ba.CopyTo(bytes, 0);
            for (int a = 0; a <= values.GetUpperBound(0); a++)
                values[a] = BitConverter.ToInt32(bytes, a * 4);
            return values;

It takes a BitArray as a parameter and returns an array of integers.

The end

Learning a New Programming Language

Eventually we all have to do it – programming languages wax and wane. One day you wake up and you are programming in Latin. All the cool kids have long since learned Italian.

Learning a new programming language isn’t always easy. Often it involves a paradigm shift (a paradigm is a unit of currency roughly equivalent to $.20 US) that changes some basic and fundamental concepts that you used to be surewere inviolate.

It doesn’t always even need to be a new language. For instance learning WPF(Windows Presentation Foundation) after having been a WinForms programmer for a number of years is a fundamentally different experience. Learning WPF is as difficult to learn as a completely new language and platform.

The new language may not be as good as the old. You might not be as productive in it. Certain things will undoubtedly be easier in the new language. Other things might even be more difficult. You might not like it aesthetically as much as the old language.

But, you know what? It doesn’t matter. This is the new language. All the cool kids are using it, and if you don’t want to be left behind you need to make the effort and learn it. And not just learn how to do it, but learn how to become good with it. Master the damned thing.

So here I present my sure fire method of mastery for any programming language. Note that it is task based and generic. With today’s new fad of DSLs (Domain Specific Languages) it is possible that your new language may not be able to accomplish some of the tasks below. It is possible that some of the tasks are trivially easy or ridiculously hard in the new language.

That’s fine. Replace these tasks with equivalent tasks of your own. Let me knowif you come up with any interesting ones.

From the list below, pick two beginner level tasks, two intermediate tasks and two advanced task.

The beginner level tasks are designed to be easy and to teach the rudiments of any programming language.

The intermediate tasks are slightly more difficult and often involve special techniques that will push you beyond your normal comfort zone.

The expert tasks should be hard to do in a language you are expert in. They use potentially uncommon techniques or techniques that are not normally accessible. Often their might be an easy way to accomplish this task. But in this case, and probably this case only, you will want to choose the harder way.

Beginner Tasks

1.  Rewrite Notepad.exe. Create an application that can open a text file, display it, allow it to be edited, and save it back to the file system.

2.  Rewrite Calc.exe. Create an application that adds, multiplies, subtracts, and divides numbers. Add more math functions if you wish.

3.  Transubstantiation. Create an application that can translate inputted numbers from Binary to Octal to Hex to Decimal, and all permutations thereof.

4.  Recreate an algorithm. Choose a quick sort, a binary search, a cyclic redundancy check, or a simple genetic algorithm in your new programming language. If the new language already has these things built in, do not use the built in versions.

Intermediate Tasks

1.  Simple PIM. Create a Personal Information Manager that will track contacts, random notes, and events. Make it pop up alerts when an event is scheduled if possible.

2.  Simple Defect Tracker. Every programmer ever has thought they could write a better bug tracker. So do it. Save defects to a database. Allow multiple people to log in. Allow a customizable defect workflow. Allow customizable fields on the defect form.

3.  Graphing Calculator. Expand your calc.exe from above to display a simple graph of a function.

4.  Simple Database. Create a simple, indexed, data store that allows you to store named record types to disk (or the equivalent) and to retrieve them back on demand. This could be an extension of the Recreate an Algorithm task from the beginner list.

5.  Design Patterns. Implement all of the GoF patterns in the new language.

Expert Tasks

1.  Syntax Highlighter. Parse your new language and redisplay the text nicely syntax highlighted. Of course, use your new language to do the parsing.

2.  Recreate MS Paint. Let the user draw and paint and save designs.

3.  Rewrite Task Manager. The windows task manager is a great and useful application. Recreate it using your new language. How cool would a web based task manager be?

4.  Simple Spreadsheet. Let users enter formulas (in your new language if possible!) in cells and automatically calculate the values. Perhaps extend your graphing calculator from the intermediate tasks.

The end