Enum.HasFlag

In not-so-new news, I just found out about .NET 4’s Enum.HasFlag method. This nice little method makes life with flags a little bit cleaner since you can use it instead of performing a bitwise AND and compare.

[Flags]
enum Options
{
	None = 0,
	A = 1,
	B = 2,
	C = 4
}

void Main()
{
	var options = Options.A | Options.C;
	
	// old (bitwise AND and compare)
	Console.WriteLine("A:{0}", (options & Options.A) == Options.A);
	Console.WriteLine("B:{0}", (options & Options.B) == Options.B);
	Console.WriteLine("C:{0}", (options & Options.C) == Options.C);
	
	// new (HasFlag method)
	Console.WriteLine("A:{0}", options.HasFlag(Options.A));
	Console.WriteLine("B:{0}", options.HasFlag(Options.B));
	Console.WriteLine("C:{0}", options.HasFlag(Options.C));
}

/* Output:
A:True
B:False
C:True
A:True
B:False
C:True
 */
About these ads

Blurry Monitors in Windows 8.1

As mentioned in yesterday’s post, I’ve been rocking a new Lenovo T440S. I noticed that whenever I attached an extra monitor, the display would get blurry. Some quick internet research suggests that this may be related to Windows 8.1 auto-scaling that attempts to make things on different screens appear to be the same size. Luckily, Microsoft has published a support article that offers two solutions to this problem: disabling DPI virtualization for specific applications or lowering the Windows DPI setting.

Both solutions worked for me, but I opted for #2 since I’m not particularly interested in dealing with the problem on a per-application basis.

Here’s the short version of how to do that:

  1. Go to Control Panel > Appearance and Personalization > Display
  2. Check Let me choose one scaling level for all my displays
  3. Optional – Choose the scaling percentage to use (I use Smaller – 100%)
  4. Click Apply

Reverse Fn/Function Keys with FnLock

I got a new laptop a few weeks back–a Lenovo T440s–and the only gripe I’ve had with it is that the F-keys are disabled by default in favor of the function keys (volume +/-, brightness +/-, etc.). Maybe that’s good for the average user, but I need to use F-key shortcuts a lot more than I need to adjust the volume, so this I’ve found this “feature” to be quite annoying.

I ran into a similar problem when I bought the Touch Cover 2 for my Surface RT. It had the same problem, but I chose to ignore it since I was mostly using the tablet for emails and web browsing. However, I did recall reading somewhere that there was a way to reverse the default functionality.

I headed over to Google with that in mind. A quick search revealed that there was, in fact, a way to toggle the “mode” by using a keyboard shortcut: Fn+Esc. I tried it out, and two nice things happened. First, I got an on-screen notification that FnLock was enabled. Second, there’s a little, green light that’s now lit on the Fn screen (similar to Caps Lock on a lot of keyboards). I didn’t notice it at the time, but as I’m writing this article, I see that “FnLk” shows as the Fn-shortcut on the Esc key, which is great because I was just thinking it’d be nice if they made this feature a little more obvious!

Excited by my newly-found FnLock shortcut, I headed over to my Surface to see if the same shortcut would work there. It did not, but another quick Google search showed me that I could use Fn+CapsLock to achieve the same thing. It’s not the universal shortcut that I was hoping it’d be, but at least I can get the functionality I want on both computers!

Sorry if I look dumb, this was sent from my phone

I’m sure this isn’t anything new, but it’s something I’ve noticed several times in the past few weeks: email signatures that say, “This email was sent from my phone, please excuse typos and brevity.” I already find it borderline offensive when somebody sends me a sloppy email with misspelled or abbreviated words and broken sentences, but to then suggest that I overlook these “mistakes” is enough to set me off.

Yes, it’s harder to type quickly and accurately from your smartphone. There’s nothing that prevents you from reading what you wrote to determine whether or not you sound like a moron, though. Nobody’s reading your horrible email and then getting to the signature and saying, “Ohhhhh, it’s just because they sent it from their phone. Whew! I was beginning to think they were dumb or didn’t care about the quality of what they produce, but it’s actually because they’re being ultra-responsive to my needs by replying on the go.” What people are really going to think is that you don’t know better or don’t care about your mistakes, and I’m not sure which is actually worse.

So, instead of letting the world know that you probably know better–and I assure you, the world doesn’t think you do–but you just don’t care, how about you take the time to READ what you’re sending and correct mistakes that you notice? Yes, you’ll probably make a typo here and there or misuse/forget to use a comma, but that’s bound to happen regardless of how you’re communicating. It’s okay, and it’s better than acknowledging that you probably sound like an idiot but just don’t care.

At the end of the day, people who care about spelling, grammar, and professionalism are going to judge you regardless of what your email signature suggests. What you should really do is just slow down and review what you produce. If you find that you just can’t write a high-quality email from your phone, I suggest you stop trying and reserve yourself to only sending emails from a computer. If it’s something that really, truly can’t wait, email is probably not the most effective means of communication, anyway. (Hint: It is incredibly likely that your phone has a phone feature.)

Database SpecFlow Scenarios and TransactionScope

My team has been using SpecFlow to verify that our data access components are working correctly. In an effort to write repeatable tests that won’t pollute the database, we’ve decided to wrap each scenario in a transaction.

Creating and rolling back the transactions is very simple to do with SpecFlow’s BeforeScenario & AfterScenario hooks.

using System.Transactions;

[Binding]
public class TransactionHooks
{
    private TransactionScope _transactionScope;

    [BeforeScenario]
    public void BeforeScenario()
    {
        _transactionScope = new TransactionScope();
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _transactionScope.Dispose();
    }
}

By including these hooks, you’re placing your test scenarios in the loving embrace of a transaction scope. The act of instantiating the TransactionScope will update the ambient transaction that will be used by all subsequent code unless you explicitly tell it to do otherwise.

If you’re testing code that uses TransactionScope itself, using TransactionScopeOption.Required will allow the code to use the ambient transaction if one exists. Note that this is the default value, so it’s what you’re using if you’re not explicitly specifying an option. However, the other TransactionScopeOption values will cause code to execute outside your test scenario’s ambient transaction by either creating a new/different transaction (RequiredNew) or executing outside the transaction (Suppress).

Why are you doing that?

We’ve all been there before: the boss approaches and asks you to do something that seems useless. You don’t ask questions. You just do it because they’re the boss, and that’s what they asked you to do.

“Why are you doing that?”
“I don’t know.”
“It seems dumb, doesn’t it?”
“Yes.”
“Then why are you doing that?”
“Boss told me to.”

Ugh. Don’t you see? Everybody loses when this happens! You’re doing something you perceive as useless, so you probably feel like you’re time is being wasted. You’re also probably not putting forth your best effort since you’re doing something you think is useless. Plus, you don’t understand the reason you’re doing it, so there’s a good chance you might overlook something that’s relevant to the actual goal. So even if you’re trying your best, you might not be as effective as you could be. Maybe there’s a better way to accomplish the goal than what you’re being asked to do, but how can you know without having any awareness of said goal?

I see this all the time, and it drives me CRAZY. It’s sad, too, because the solution is so simple: ask questions. Don’t do something without understanding why you’re doing it. The worst reason you can have for doing something is because somebody told you to do it. The next worst reason is that it’s what you’ve always done. If you’ve been given an assignment, and you don’t understand why you’re doing it, don’t just do it: ask some damn questions! You’re smart. You have ideas. If you understand the problem you might be able to come up with some smart ideas about how to solve it. (And if you can’t, at least you’ll understand why you’re doing that dumb thing you’re doing.)

You might be able to do an okay job by just doing what you’re asked to do without knowing why you’re doing it. You’ll probably do a better job if you know what’s trying to be accomplished, but you’ll do your best work if you can somehow find a way actually care about the problem. Mindlessly doing what your told is a fantastic way to be mediocre and not have a satisfying career. Being engaged plays an important role in both performance and satisfaction. You must understand a problem in order to care about it, and only when you care will you become truly engaged–unlocking your full potential, producing your best work, and feeling maximum satisfaction for what you’ve accomplished.

Paste as Plain Text in Windows Store App

I was working on a Windows Store app that used a RichEditBox for text entry. Everything worked great until I pasted some formatted text into it. The dumb thing kept all the formatting! Okay, I guess that’s probably good since it would be pretty annoying if you wanted the formatting, but I didn’t.

Good news, though: it’s easy to remove that formatting and paste as plain text. So easy, in fact, that you can do it in a single line. RichEditBox has an ITextDocument property (Document) that has an ITextRange property (Selection). And guess what ITextRange has? A Paste method! However, this is where it got a little tricky for me because Paste takes an integer parameter, and it took me some googling to find a list of valid values. (Sorry, text-pasting enthusiasts, but you can’t use DataFormats in a Windows Store app.) In the end, I was able to find what I needed here.

Once you’ve got the code, you just hook up a handler to the RichEditBox’s Paste event and–voila!–pasting as plain text.

XAML:

<RichEditBox Paste="OnPaste" />

Code-behind:

private void OnPaste(object sender, TextControlPasteEventArgs e)
{
    var editBox = sender as RichEditBox;
    if (editBox.Document.CanPaste())
    {
        editBox.Document.Selection.Paste(1);
        e.Handled = true;
    }
}