Re: confused about null reference compile error
- From: "Michel Prévost" <michel.prevost_TAKEOUTTHISPART@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 26 Apr 2005 12:02:40 -0400
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
"Steve Richter" <StephenRichter@xxxxxxxxx> wrote in message
news:1114530561.695541.57240@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>
.
- Follow-Ups:
- Re: confused about null reference compile error
- From: Angrez Singh
- Re: confused about null reference compile error
- From: Steve Richter
- Re: confused about null reference compile error
- References:
- confused about null reference compile error
- From: Steve Richter
- confused about null reference compile error
- Prev by Date: Re: open new browser window - asp.net
- Next by Date: Re: File Systesm Object
- Previous by thread: confused about null reference compile error
- Next by thread: Re: confused about null reference compile error
- Index(es):
Relevant Pages
|