The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Handling a null guid

Hi,

I really cant figure this one out. I have the following code:

if (userInfo.ProviderUserKey.ToString() != null)
        {
            userID = (Guid)userInfo.ProviderUserKey;
        }

Which is fine unless the guid value:

userInfo.ProviderUserKey

is null, then it breaks.

Any ideas appreciated.

Thanks
Kieran
Kieran
Friday, July 07, 2006
 
 
Do the test before the string conversion?
nullable
Friday, July 07, 2006
 
 
I tried this. It tells me that it needs to be a string to allow to check != null.

Thanks
Kieran
Kieran
Friday, July 07, 2006
 
 
"if (userInfo.ProviderUserKey.ToString() != null)
        {
            userID = (Guid)userInfo.ProviderUserKey;
        }
"

How about checking userInfo.ProviderUserKey != null first?

Ken
Ken
Friday, July 07, 2006
 
 
When I do this:

        if (userInfo.ProviderUserKey != null)
        {
            userID = userInfo.ProviderUserKey.ToString();
        }

'Object reference not set to an instance of an object.'

at runtime.

Kieran
Kieran
Friday, July 07, 2006
 
 
Looks to me like it's userInfo that's null and causing the problem.
MB Send private email
Friday, July 07, 2006
 
 
A Guid cannot be null (it's a value, not a reference); at worst it can equal Guid.Empty.
Christopher Wells Send private email
Friday, July 07, 2006
 
 
Thanks! I think you might just be right. This seems to work:

        if (userInfo != null)
        {
            userID = userInfo.ProviderUserKey.ToString();
        }

I wonder why that works but this doesn't:

        if (userInfo.ProviderUserKey != null)
        {
            userID = userInfo.ProviderUserKey.ToString();
        }

Many Thanks
Kieran
Kieran
Friday, July 07, 2006
 
 
There are a few of oddities in your code.

First, System.Guid is a struct, not a class.  Therefore, it can't be null.  Second, why call ToString and test that instead of testing the actual property?  Third, you're casting ProviderUserKey to Guid, which makes me think you're storing it as an object (or a string, which would make the ToString call even more strange).  Why?
Posted by me
Friday, July 07, 2006
 
 
"I wonder why that works but this doesn't:

if (userInfo.ProviderUserKey != null)
{
  userID = userInfo.ProviderUserKey.ToString();
}"

Because if userInfo is null, you can't access any of its properties.  That'd be like saying, "There is no building.  How many floors does it have?"
Posted by me
Friday, July 07, 2006
 
 
I'm reading the guid from the aspnet_users table. If the user is not logged in then the userInfo object is null. I mistakenly checked the property rather than the object for null. I think that's where I went wrong.

Does that make sense?

Kieran
Kieran
Friday, July 07, 2006
 
 
>>Third, you're casting ProviderUserKey to Guid, which makes me think you're storing it as an object (or a string, which would make the ToString call even more strange).  Why?

I tried removing the cast:

Guid userID = Guid.Empty;

        MembershipUser userInfo = Membership.GetUser(HttpContext.Current.User.Identity.Name);

        if (userInfo != null)
        {
            userID = userInfo.ProviderUserKey;
        }

I get:

Error    2    Cannot implicitly convert type 'object' to 'System.Guid'. An explicit conversion exists (are you missing a cast?)

Kieran
Kieran
Friday, July 07, 2006
 
 
For your own sake, stop writing code by copy+paste from others code samples and instead spend some time reading a C# book first and then an ASP book. You will save time in the long run, trust me.
Mellowman Send private email
Friday, July 07, 2006
 
 
How have you figured from this that I have or haven't read many c# and 'asp.net' books?
Kieran
Friday, July 07, 2006
 
 
Honestly no offence intended Kieran, but Mellowman is on the ball.
MB Send private email
Friday, July 07, 2006
 
 
None taken :)
Kieran
Friday, July 07, 2006
 
 
A more tactful way of what Mellowman is saying is, what are you trying to accomplish and why are you trying to do it that way?

Take this example...

if (userInfo.ProviderUserKey != null) {
  userID = userInfo.ProviderUserKey.ToString();
}

You could basically just do (in psuedo code)...

userID = new String( userInfo.ProviderUserKey );
if ( userID is null ) or ( userID == "" ) {
  error( "No user id was provided." );
  exit;
}
TheDavid
Friday, July 07, 2006
 
 
Thanks guys, have a great weekend!
Kieran
Friday, July 07, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz