Null on property

S

shapper

Hello,

I created a custom Profile provider as follows:

public class Profile : ProfileBase {

[SettingsAllowAnonymous(false)]
public Bio Bio { get; set; }

[SettingsAllowAnonymous(true)]
public Visitor Visitor { get; set; }

public static Profile Get() {
return Create(Membership.GetUser().UserName) as Profile;
}
public static Profile Get(string username) {
return Create(username) as Profile;
}

}

Bio is a property available only for users that has signed on.
Visitor is a property available for all users.

Every time a user accesses a page I have:

Profile profile = Profile.Get(User.Identity.Name);
CultureInfo culture = profile.Visitor.Culture;

Of course if the user is accessing the page for the first time Visitor
is not defined and I get an error:
"Object reference not set to an instance of an object." on code line
2.

I could test if Visitor is null and if it is define a value for its
Culture.

But should I make this on the property itself? If yes, what is the
correct way to do this?

Thanks,
Miguel
 
S

shapper

Hello,

I created a custom Profile provider as follows:

  public class Profile : ProfileBase {

    [SettingsAllowAnonymous(false)]
    public Bio Bio { get; set; }

    [SettingsAllowAnonymous(true)]
    public Visitor Visitor { get; set; }

    public static Profile Get() {
      return Create(Membership.GetUser().UserName) as Profile;
    }
    public static Profile Get(string username) {
      return Create(username) as Profile;
    }

  }

Bio is a property available only for users that has signed on.
Visitor is a property available for all users.

Every time a user accesses a page I have:

      Profile profile = Profile.Get(User.Identity.Name);
      CultureInfo culture = profile.Visitor.Culture;

Of course if the user is accessing the page for the first time Visitor
is not defined and I get an error:
"Object reference not set to an instance of an object." on code line
2.

I could test if Visitor is null and if it is define a value for its
Culture.

But should I make this on the property itself? If yes, what is the
correct way to do this?

Thanks,
Miguel

I forgot to say:

Bio and Visitor are

[Serializable()]
public class Visitor {
public CultureInfo Culture { get; set; }
public List<Guid> Polls { get; set; }
}

[Serializable()]
public class Bio {
public DateTime Birthday { get; set; }
public Gender Gender { get; set; }
public string Name { get; set; }
}

I think I am missing some kind of initialization on my classes.

Thanks,
Miguel
 

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

Top