Connecting Tech Pros Worldwide Forums | Help | Site Map

Comparing structs and <null>

Bob Gregory
Guest
 
Posts: n/a
#1: Nov 16 '05
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

Bob Grommes
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Comparing structs and <null>


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 Gregory" <bobgregory@ppsltd.net> wrote in message
news:aba9c4c4.0409031652.7e6ef230@posting.google.c om...[color=blue]
> 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[/color]


Lateralus [MCAD]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Comparing structs and <null>


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.

--
Lateralus [MCAD]


"Bob Gregory" <bobgregory@ppsltd.net> wrote in message
news:aba9c4c4.0409031652.7e6ef230@posting.google.c om...[color=blue]
> 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[/color]


Lateralus [MCAD]
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Comparing structs and <null>


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 [MCAD]


"Bob Gregory" <bobgregory@ppsltd.net> wrote in message
news:aba9c4c4.0409031652.7e6ef230@posting.google.c om...[color=blue]
> 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[/color]


Bob Gregory
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Comparing structs and <null>


"Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message news:<#aTXMUikEHA.3372@TK2MSFTNGP09.phx.gbl>...[color=blue]
> Bob,
> I suppose you could take a look at overloading operators....but it could
> get ugly.[/color]


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?
[color=blue]
>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.[/color]

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.
[color=blue]
>
> HTH
>[/color]

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


-- Bob

[color=blue]
> "Bob Gregory" <bobgregory@ppsltd.net> wrote in message
> news:aba9c4c4.0409031652.7e6ef230@posting.google.c om...[color=green]
> > 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[/color][/color]
Lateralus [MCAD]
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Comparing structs and <null>


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=e...com%26rnum%3D2

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.

--
Lateralus [MCAD]


"Bob Gregory" <bobgregory@ppsltd.net> wrote in message
news:aba9c4c4.0409040703.313da156@posting.google.c om...[color=blue]
> "Lateralus [MCAD]" <dnorm252_at_yahoo.com> wrote in message
> news:<#aTXMUikEHA.3372@TK2MSFTNGP09.phx.gbl>...[color=green]
>> Bob,
>> I suppose you could take a look at overloading operators....but it
>> could
>> get ugly.[/color]
>
>
> 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?
>[color=green]
>>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.[/color]
>
> 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.
>[color=green]
>>
>> HTH
>>[/color]
>
> You confirmed what I'd figured out, and you have letters after your
> name, so thanks a lot :)
>
>
> -- Bob
>
>[color=green]
>> "Bob Gregory" <bobgregory@ppsltd.net> wrote in message
>> news:aba9c4c4.0409031652.7e6ef230@posting.google.c om...[color=darkred]
>> > 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[/color][/color][/color]


Closed Thread