Null on property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Null on property

    Hello,

    I created a custom Profile provider as follows:

    public class Profile : ProfileBase {

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

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

    public static Profile Get() {
    return Create(Membersh ip.GetUser().Us erName) 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(Use r.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


  • shapper

    #2
    Re: Null on property

    On Oct 10, 2:00 pm, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    I created a custom Profile provider as follows:
    >
      public class Profile : ProfileBase {
    >
        [SettingsAllowAn onymous(false)]
        public Bio Bio { get; set; }
    >
        [SettingsAllowAn onymous(true)]
        public Visitor Visitor { get; set; }
    >
        public static Profile Get() {
          return Create(Membersh ip.GetUser().Us erName) 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(Use r.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<GuidPolls { 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

    Comment

    Working...