We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)

mathieu_cupryk
on
Medium Priority
2,736 Views
Last Modified: 2012-05-06
public static System.Int32 Age
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Age") as System.Int32; }
            set { HttpContext.Current.Profile.SetPropertyValue("Age", value); }
        }

Error      1      The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type)      C:\inetpub\wwwroot\OmegaLove\OL_Web\App_Code\UserProfile.cs      49      26      C:\...\OL_Web\

using System;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Web.Caching;
 
public static class UserProfile
{
    public class Personal
    {
        public static System.String UserName
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("UserName") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("UserName", value); }
        }
 
        public static System.String Password
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Password") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Password", value); }
        }
 
        public static System.String Email
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Email") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Email", value); }
        }
 
        public static System.String FirstName
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("FirstName") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("FirstName", value); }
        }
 
        public static System.String LastName
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("LastName") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("LastName", value); }
        }
 
        public static System.DateTime DOB
        {
            get { return (System.DateTime)HttpContext.Current.Profile.GetPropertyValue("DOB"); }
            set { HttpContext.Current.Profile.SetPropertyValue("DOB", value); }
        }
 
        public static System.Int32 Age
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Age") as System.Int32; }
            set { HttpContext.Current.Profile.SetPropertyValue("Age", value); }
        }
 
        public static System.String Gender
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Gender") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Gender", value); }
        }
 
        public static System.String Seeking
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Seeking") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Seeking", value); }
        }
 
        public static System.String ConfirmationGUID
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("ConfirmationGUID") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("ConfirmationGUID", value); }
        }
 
        public static System.String UserIP
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("UserIP") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("UserIP", value); }
        }
 
        public static System.String SessionID
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("SessionID") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("SessionID", value); }
        }
 
    }
    public class Address
    {
        public static System.String Country
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Country") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Country", value); }
        }
 
        public static System.String Region
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Region") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Region", value); }
        }
 
        public static System.String City
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("City") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("City", value); }
        }
 
        public static System.String ZipCode
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("ZipCode") as System.String; }
            set { HttpContext.Current.Profile.SetPropertyValue("ZipCode", value); }
        }
 
    }
    public class Preferences
    {
        public static String Theme
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Theme") as String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Theme", value); }
        }
 
        public static String Culture
        {
            get { return HttpContext.Current.Profile.GetPropertyValue("Culture") as String; }
            set { HttpContext.Current.Profile.SetPropertyValue("Culture", value); }
        }
 
    }
    public static void Save()
    {
        HttpContext.Current.Profile.Save();
    }
}

Open in new window

Comment
Watch Question

Try this
    public static class UserProfile
    {
        public class Personal
        {
            public static System.String UserName
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("UserName") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("UserName", value); }
            }
 
            public static System.String Password
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Password") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Password", value); }
            }
 
            public static System.String Email
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Email") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Email", value); }
            }
 
            public static System.String FirstName
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("FirstName") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("FirstName", value); }
            }
 
            public static System.String LastName
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("LastName") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("LastName", value); }
            }
 
            public static System.DateTime DOB
            {
                get { return (System.DateTime)HttpContext.Current.Profile.GetPropertyValue("DOB"); }
                set { HttpContext.Current.Profile.SetPropertyValue("DOB", value); }
            }
 
            public static System.Int32? Age
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Age") as System.Int32?; }
                set { HttpContext.Current.Profile.SetPropertyValue("Age", value); }
            }
 
            public static System.String Gender
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Gender") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Gender", value); }
            }
 
            public static System.String Seeking
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Seeking") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Seeking", value); }
            }
 
            public static System.String ConfirmationGUID
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("ConfirmationGUID") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("ConfirmationGUID", value); }
            }
 
            public static System.String UserIP
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("UserIP") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("UserIP", value); }
            }
 
            public static System.String SessionID
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("SessionID") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("SessionID", value); }
            }
 
        }
        public class Address
        {
            public static System.String Country
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Country") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Country", value); }
            }
 
            public static System.String Region
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Region") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Region", value); }
            }
 
            public static System.String City
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("City") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("City", value); }
            }
 
            public static System.String ZipCode
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("ZipCode") as System.String; }
                set { HttpContext.Current.Profile.SetPropertyValue("ZipCode", value); }
            }
 
        }
        public class Preferences
        {
            public static String Theme
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Theme") as String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Theme", value); }
            }
 
            public static String Culture
            {
                get { return HttpContext.Current.Profile.GetPropertyValue("Culture") as String; }
                set { HttpContext.Current.Profile.SetPropertyValue("Culture", value); }
            }
 
        }
        public static void Save()
        {
            HttpContext.Current.Profile.Save();
        }
    }

Open in new window

Make it as a nullable type because HttpContext.Current.Profile.GetPropertyValue("Age") can be a null value and System.Int32 cannot be a null value because it is a non nullable value type.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
excellent.
I will be posting two questions one with on how I can show all the information about a user. with that class and secondly one with serialization of that object,
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.