-3

here's another quirk in .net 4.0 i found:

When i want to check if a variable, which holds a System.Drawing.Brush, holds a certain Brush, like:

using System.Drawing;

HisCell.Col == Brushes.White

it sometimes fails, despite the fact I assigned Brushes.White to HisCell.Col earlier.

How do i work around this bug?

Magnus
  • 45,362
  • 8
  • 80
  • 118
user2762996
  • 566
  • 1
  • 5
  • 16
  • 4
    Use a debugger. If it's not Brushes.White, what else is it? Maybe it's your fault? A bug in the .NET Framework is not impossible, but a bug in your own code is much more likely. – nvoigt Mar 03 '14 at 15:57
  • Whenever something fails 'sometimes' suspect your code! – Marius Bancila Mar 03 '14 at 16:02
  • You don't want to work around the bug, you want to *fix* the bug. Chances are, the bug is in *your* code. Find every place that changes `HisCell.Col` and chances are you will find that some other code is modifying its reference. – NathanAldenSr Mar 03 '14 at 16:08
  • 1
    Not disposing System.Drawing objects is a standard flaw in a .NET program. Works for quite a while, every seems to works just fine. Until the number of repaints out-paces the rate at which the garbage collector runs, kaboom then. Don't dispose Brushes.White btw, the other ones. – Hans Passant Mar 03 '14 at 16:09

1 Answers1

1

The Brushes class creates thread-local copies of its Brush objects. So, if you did a comparison of Brushes.White to a value that was set on another thread, they'd be different objects and result in false in equality because Brush does not implement equality so reference equality would be performed.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98