confused about null reference compile error

S

Steve Richter

this code compiles ok:
public string AssureAnonUID( )
{
if (Session["AnonUID"] == null)
Session["AnonUID"] = Guid.NewGuid();
return Session["AnonUID"].ToString();
}

but this code does not:
public string AssureAnonUID( )
{
Guid guid = (Guid) Session["AnonUID"] ;
if ( guid == null )
{
guid = Guid.NewGuid( ) ;
Session["AnonUID"] = guid ;
}
return guid.ToString( ) ;
}

c:\inetpub\wwwroot\IBuyAdventure\components\stdpage.cs(29): Operator
'==' cannot be applied to operands of type 'System.Guid' and '<null>'

Is that because System.Guid is a struct? If so, how does the object
returned by
Session["AnonUID"]
get converted to the struct by this stmt:
Guid guid = (Guid) Session["AnonUID"] ;

if this is the case, it seems inconsistent.

thanks,

-Steve
 
M

Michel Prévost

This is because Guid is a value-type, it can never be null.

Would it compile, this would throw an exception probably. Just stick to your
first version, it is the best one. Also, you may consider storing "AnonUID"
in a global constant (but you probably already know that).

Michel
 
S

Steve Richter

Michel said:
This is because Guid is a value-type, it can never be null.

Would it compile, this would throw an exception probably. Just stick to your
first version, it is the best one. Also, you may consider storing "AnonUID"
in a global constant (but you probably already know that).

Michel

so this stmt:
Session["AnonUID"] = Guid.NewGuid();

returns a Guid struct value type and stores it as a reference type in
the Session whatever collection?

then, this one:
return Session["AnonUID"].ToString();

is acting on a reference to the base System.Object of the Guid struct?
Is the ToString interface call acting on a value or reference type?

I guess I have to do more reading :)

thanks Michel,

-Steve
Steve Richter said:
this code compiles ok:
public string AssureAnonUID( )
{
if (Session["AnonUID"] == null)
Session["AnonUID"] = Guid.NewGuid();
return Session["AnonUID"].ToString();
}

but this code does not:
public string AssureAnonUID( )
{
Guid guid = (Guid) Session["AnonUID"] ;
if ( guid == null )
{
guid = Guid.NewGuid( ) ;
Session["AnonUID"] = guid ;
}
return guid.ToString( ) ;
}

c:\inetpub\wwwroot\IBuyAdventure\components\stdpage.cs(29): Operator
'==' cannot be applied to operands of type 'System.Guid' and
' said:
Is that because System.Guid is a struct? If so, how does the object
returned by
Session["AnonUID"]
get converted to the struct by this stmt:
Guid guid = (Guid) Session["AnonUID"] ;

if this is the case, it seems inconsistent.

thanks,

-Steve
 
A

Angrez Singh

Hi,

The thing is Session["string"] returns an object of type "Object" which
is of reference type and can be null.
On the other hand when you type cast it to struct ( which is value type
) so you can't compare it with null. Just stick to first one.

Regards,
Angrez
 
S

Steve Richter

Angrez said:
Hi,

The thing is Session["string"] returns an object of type "Object" which
is of reference type and can be null.
On the other hand when you type cast it to struct ( which is value type
) so you can't compare it with null. Just stick to first one.

Regards,
Angrez

oh, ok, the typecast did it.

new version:
System.Object guid = Session["AnonUID"] ;
if ( guid == null )
{
guid = Guid.NewGuid( ) ;
Session["AnonUID"] = guid ;
}
return guid.ToString( ) ;

thank you,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top