Re: confused about null reference compile error

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
>


.



Relevant Pages

  • confused about null reference compile error
    ... this code compiles ok: ... public string AssureAnonUID() ... Is that because System.Guid is a struct? ... Prev by Date: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: associative array, container, mapping, terminology
    ... |> is a pointer, I see that as a kind of type, but also specific to what it ... |> different kind of struct I consider to really be two different types. ... | but which kind isn't known at compile time, ... I'm looking at having a mapping that is homogenous keys and homogenous data, ...
    (comp.programming)
  • Re: Generating a simple hand-coded like recursive descent parser
    ... include compound words due to intrinsic functions. ... What do you mean by keyword combinations? ... "compile", directive on what compiler options will be used ... struct ASTNode { ...
    (comp.compilers)
  • Re: Need help in Including/converting C files in C++ project
    ... I never realized that I should set the "Compile As" ... > struct X ... > differs from X in the typedef. ... The name of the first struct is X in C++, ...
    (microsoft.public.vc.language)
  • Re: ANSI C Compilation linking problem
    ... I have the files group.h and group.c that use a struct define in logic.h and two fonctions define in logic.c. ... when the program compile, it is able to find the reference to the ... So the logic.o wasnt really compile and he forgot that it wasnt complete and still tryed to link it. ...
    (comp.lang.c)