Sunday, October 10, 2010

C# Equals() Is Not Equal To operator==(); or So You Used To Be A C++ Programmer

C++ was my bread and butter for a very, very long time, and for the most part that has been invaluable in helping me learn a model of software execution that very closely models the actual implementation in silicon (thanks to the original C model).  And in C++, there is no default equality operator (operator ==) for objects.  This means code like:

class A { /* members */ };
A one, two;
if (one == two) { /* they're equal */ }

will not compile.  This is good; you know you need to implement an operator ==() to proceed.

And now C# is my mainstay, partly of necessity, and partly (although I hate to admit it) because it is a productivity booster.  So... what do you know, this compiles:

class A { /* members */ }
A one = new A();
A two = new A();
if (one == two) { /* they're equal...? */ }

But how does it assess the equality of the two objects?  Here's the fun bit: for object variables in C# that do not provide their own, operator ==() generally compares the references for equality.  Thus it will only return true if both variables refer to the same instance of the class (the equivalent of comparing the addresses of the C++ objects, like &one == &two).  Surprise!  And here I thought there would be some clever memberwise comparison going on.

I use Dictionary<K,V> a lot, even for my own classes as keys.  But read the Dictionary docs carefully: you'll need to implement IEquatable<T> in your classes if you properly want to use them as keys!  And if you read the IEquatable<T> docs carefully, you'll see you should really implement three member functions:

bool Equals(T rhs)
virtual override bool Equals(object rhs)
virtual override int GetHashCode()

That was definitely one of those "ya learn something new every day" days.  Such fun.

Thursday, September 2, 2010

Serializing A C# Object Might Drop Your Attribute If You're Not Careful

I was trying to serialize an object the class for which had been created by the .NET wsdl tool built in to Visual Studio 2010; the object represented an element (ContactInfo) with a single optional attribute (SendData) of type xs:boolean.  I wanted to generate the attribute so that it looked like this:

<ContactInfo SendData="true" />

so I did this in the C# with the corresponding wsdl-generated object: 

contactInfo.SendData = true;

But this generated only: 

<ContactInfo />

What the heck?  I set the property to true; why wasn't the corresponding attribute appearing?  After googling around, a bit, I found this (https://issues.apache.org/jira/browse/AXIS-2155): 

when you use .NET to generate client proxy classes(in our case we generate C# classes) the wsdl tool in .NET generates a XXXspecified along with XXX in the .cs file for primitive types to indicate whether they are specified or not. Eg.,

if the element in schema looks like this:

        <xsd:element name="zip" type="xsd:int" minOccurs="0"/>

then the file generate will have attributes like this in C#:

        private int zipField;

        private bool zipFieldSpecified;

So I really shouldn't have ignored that SendDataSpecified property of my contactInfo object.  It makes sense; if it's a primitive type, there is not a way to indicate not to include the optional element since you can't assign null to a primitive type (and Nullable doesn't count).  So now I do this and I'm all happy: 

contactInfo.SendDataSpecified = true;
contactInfo.SendData = true;

makes 

<ContactInfo SendData="true" />

Thursday, August 26, 2010

File Size On Disk: The Next Generation

Several trillion years ago I wrote an OWL-based C++ application (yeah, that's the Borland [now Embarcadero] Object Window Library, a fondly-missed object-oriented wrapper around the Windows UI that put MFC to shame, now maintained in open-source as OWLNext) which I meant as a personal tool to try to find out where the disk space on a drive was being used; I called it Disk Inspector.  It served me well for quite a long time.  But that application is now not behaving well on recent versions of Windows and seems to be creating Windows Data Execution Prevention errors upon execution .

So I've been meaning to update the application, preferably in C#, but the framework just doesn't offer tools with which to determine disk usage accurately enough.  Googling tonight has yielded this gem: http://foldersize.sourceforge.net/.  The source code is available, and I fully intend to snarf it to re-create Disk Inspector TNG (or something less silly) in C++... but now... what UI framework in C++?  Do I p/invoke it from .NET?  Scary.  Do I learn whatever VS2010's C++ UI normally uses?  I can't imagine doing so.  Download Turbo C++ (if we can still do that) and use VCL again?    What to do, what to do...

[Update 7/15/2011]
What to do?  Why, you lazy bugger, just keep googling for free software that does what you want it to do; in this case, use Disktective.  (Cheater pants.)

Monday, August 16, 2010

Long Web Service String Parameters (As For XML Strings) Too Long

While testing a web service client which was calling a method which returned a string value (intended to contain XML), I got the following error:

The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.

I was able to get around this by changing my app.config file's entry for the binding as, well, described in the error message (from 8192 to 819200):














Saturday, August 14, 2010

Windows Update Hammering Your PC Too?

So there's an ancient laptop in the kitchen we use primarily to stream music during breakfast and to run BOINC.  For the past couple of days, even given its age and small memory, it took it nearly a billion years to start its first application.  The hard drive was completely maxed out, and Task Manager noted that wuauclt.exe was taking up a HUGE chunk of memory.  I've just noticed on the other laptop the same massive performance hit, also while wuauclt.exe was chewing up the system.  I seem to recall that wuauclt is the Windows Update Automatic Update Client... what the heck is the problem here?  Can't wait until they take care of that...

Sunday, August 8, 2010

Through The Self-Looking Glass

Why Blog?

Not so long ago I had no sense of the necessity of dumping a diary into the already-overloaded-with-the-inane searchable web.  I don't know that I do fully, now, either.  But as the CTO and co-founder of the social networking site for educators edWeb.net, it certainly seems appropriate for me to have a blog (or, at worst, inappropriate for me not to have one!).

The vast majority of things I'll throw up here... spewing forth my congealed thoughts, as it were... are going to be the weird, bizarre, and painful hurdles I'll have leapt through while in the throes of software development.  The arcane and poorly documented will be what fill these "pages".

However, I have also been slowly brewing my thoughts on a non-tech page... my top 20 albums (yes, I mean CD's, for all you kids under 25 out there).  Pride, vanity, and pleasure mix well on that kind of post.

So... a deep breath, and into the digital microcosmic representation of the trajectory of one particle slowly meandering around underneath the ocean of air on this tiny rock!

Forgive me.