Comparing structs and <null>

  • Thread starter Thread starter Bob Gregory
  • Start date Start date
B

Bob Gregory

Hi all, I'm utter C# newbie, do be gentle.

VS2005 Express refuses point blank to install on my box, so I'm stuck
with C# 1.0 unless someone can point me to a C#2.0 compiler elsewhere.

I have an authentication system rigged up, which is working, but I'd
like to be able to run a test like this

<code>

user thisUser = UserDirectory.GetUserByUsername(Username);
if(thisUser==null){
// user does not exist
}
else{
// do password tests, fetch roles etc.
}

</code>

but the conditional fails 'cos I can't compare type user and <null>.

I've compromised by returning an empty user object and then testing

<code>

if(thisUser.username == null){
// etc. etc.
}

</code>

which works but /smells/ wrong... is there a better way? Will the
nullable types in C# 2.0 let me do this?

Cheers for any advice you can offer,

-- Bob
 
I take it that thisUser is an int or other value type? In that case, no you
can't set it to null.

NullableTypes in CLR 2.0 would be one answer. A magic value is another
possibility, though it offends my sense of elegance (such as it is).

--Bob Grommes
 
Bob,
I'm not sure what the result is in VS 2005, but I beleive that structs
are value types. They can't hold null values. I would just convert the
structure to a class and all of your headaches will be gone. This may not be
the best way, but I think you'll find it pretty easy to implement.
 
Bob,
I suppose you could take a look at overloading operators....but it could
get ugly. Like I said, this is an alternative, but the easier route would be
just to make it a class. Technically a struct is going to be faster than a
class, but lets be honest, it's not going to bring your systems memory to
it's knees.

HTH
 
Lateralus said:
Bob,
I suppose you could take a look at overloading operators....but it could
get ugly.


That was one of the things I considered, but it did seem a little over
the top for such a simple operation. Out of curiosity, what is the
basic syntax for creating an overloaded operator to test for null?
Like I said, this is an alternative, but the easier route would be
just to make it a class. Technically a struct is going to be faster than a
class, but lets be honest, it's not going to bring your systems memory to
it's knees.

Yeah, I came to this conclusion 5 minutes after posting, with the aid
of a guide to reference and value types; that's about my average
whenever I post to Google, so I always end up feeling foolish.
Conceptually it's nicer anyway because a user is an object and not a
value, but that's what you get for trying to be clever about
performance.

You confirmed what I'd figured out, and you have letters after your
name, so thanks a lot :)


-- Bob
 
Bob,
Here is a link to a discussion that should show you some sample code.
They got around it by creating a "dummy" class, overloading the operators,
as well as the Equals() and GetHashCode() methods. You have to overload the
2 methods just mentioned if you are going to overload the "==" and "!="
comparison operators.

http://groups.google.com/groups?hl=...4%24Ji1.213079%40news20.bellglobal.com&rnum=2

The name of the thread is "c# assign null to a struct". If the link above
doesn't work, just search for that thread on google.
 
Back
Top