tags:

views:

86

answers:

1

I'm using some old code that runs a sql query as a reference.

At some point, it gets to something like:

sqlDataAdapter.Fill(dataSet);
DataRow dataRow = dataSet.Tables[0].Rows[0];
Object obj = dataRow[fieldName];

The old code does:

string output;
if (!string.IsNullOrEmpty(obj.ToString())) { output = obj.ToString(); }
else { output = "Not Available"; }

I changed them to:

output = obj as string ?? "Not Available"

But sometimes, it broke. As I suspected, it was happening breaking when the entry was an int. Casting as an int in those cases solved that problem.

Then another problem arose when there was no entry for obj[fieldName] of type int. When I stepped through the debugger, I was surprised to find that obj wasn't null. In VS, mousing over revealed it had a value {}.

What the heck is {}? How do I make a boolean test of it?

(In the old code, it appears .ToString() returns "" in this case and works as expected.)

+4  A: 

{ and } are opening and closing braces and symbolic of the start and finish of an object. Hence an empty object with no special properties is depicted in shorthand as {}. The debugger uses this notation to help you visually distinguish between an empty object, an empty string and null.

If you hover over obj[fieldName] and there is no entry for fieldName, the debugger won't care about that, it'll show the value of obj. You'll have to use the immediate window or a watch/quickwatch. The debugger will only see you hovering over obj and assume you're referring to the array itself, not the contents of the array at the specified index.

Nathan Ridley
And there isn't an `Object.Empty` for `obj == Object.Empty` test? Or will can I create a `Object comparableEmpty = new Object()`?
Kache4
No, because `Object` is a reference type, which means if it existed, comparing Object.Empty to your own object would always be false. You could possibly use `myObj.GetType().GetProperties().Count == 0` or some variation thereof, though the overhead is probably not worth it.
Nathan Ridley
Feels kinda dumb how there isn't an elegant solution for this. If it's a reference type, the reference should just be `null` like a good little reference.
Kache4
Any empty array is not null. I think you have a fundamental misunderstanding of a few basic things. I'll try to update my answer with a bit more information.
Nathan Ridley
Then you're saying that an `Object` is more like a container than a reference. A container that can only have one item and no convenient way to check to see if it's empty.
Kache4
`Object` is the base class for ALL types in .Net. You are referring to it as though it is something special. It is in fact, *less* special than any other type. It's also not a container the way you're referring to. It's just a type with no properties and no methods other than the most most basic methods shared by all types (seeing as they are inherited from `Object` anyway).
Nathan Ridley
Well, either there's a way to test if it's an empty object or not, or my only solution is to do `obj.ToString()` and see if that returns `null` or `""`
Kache4
what are you trying to achieve by testing for an empty object? I don't understand. There is no reason to do this, I think, in the context you are talking about. Are you talking about testing for an empty array?
Nathan Ridley
I've figured out what I need to do. `obj.getType().Equals(System.DBNull.Value)`. In my question, I stated that I needed a boolean for control flow to handle when an entry is empty.
Kache4