M
Mick Walker
I have the following simple class:
using System;
[Serializable]
public class Person
{
public Person()
{
}
/// <summary>
/// Creates a new person object with pre assigned values
/// </summary>
/// <param name="Fname">Persons Firstname</param>
/// <param name="Sname">Persons Surname</param>
/// <param name="Age">Persons Age</param>
/// <param name="Postcode">Persons PostCode</param>
public Person(string Fname, string Sname, int Age, string Postcode)
{
strFName = Fname;
strSName = Sname;
intAge = Age;
strPostcode = Postcode;
}
# region "Properties"
private string _strFname;
private string _strSname;
private int _intAge;
private string _strPostcode;
public string strFName
{
get {return _strFname;}
set {_strFname = value; }
}
public string strSName
{
get {return _strSname;}
set {_strSname = value;}
}
public int intAge
{
get {return _intAge; }
set {_intAge = value;}
}
public string strPostcode
{
get { return _strPostcode;}
set {_strPostcode = value; }
}
#endregion
}
And I use this class as follows:
-- Default.aspx.cs
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person("Mick", "Walker", 27, "xxxxR");
Session["SomePerson"] = myPerson;
Response.Redirect("Default2.aspx");
}
}
And in Default2.aspx
using System;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson"];
Response.Write(myPerson.strFName + "<br/>");
Response.Write(myPerson.strSName + "<br/>");
Response.Write(myPerson.intAge.ToString() + "<br/>");
Response.Write(myPerson.strPostcode + "<br/>");
}
}
This gives me the Error:
Error 1 Cannot implicitly convert type 'object' to 'Person'. An explicit
conversion exists (are you missing a cast?)
C:\Projects\StateTest\Default2.aspx.cs 17 20 C:\Projects\StateTest\
I am using SQL Server as my session provider if that makes a difference
(I did however mark the Person Object as Serializable).
Does anyone know what I am doing wrong?
using System;
[Serializable]
public class Person
{
public Person()
{
}
/// <summary>
/// Creates a new person object with pre assigned values
/// </summary>
/// <param name="Fname">Persons Firstname</param>
/// <param name="Sname">Persons Surname</param>
/// <param name="Age">Persons Age</param>
/// <param name="Postcode">Persons PostCode</param>
public Person(string Fname, string Sname, int Age, string Postcode)
{
strFName = Fname;
strSName = Sname;
intAge = Age;
strPostcode = Postcode;
}
# region "Properties"
private string _strFname;
private string _strSname;
private int _intAge;
private string _strPostcode;
public string strFName
{
get {return _strFname;}
set {_strFname = value; }
}
public string strSName
{
get {return _strSname;}
set {_strSname = value;}
}
public int intAge
{
get {return _intAge; }
set {_intAge = value;}
}
public string strPostcode
{
get { return _strPostcode;}
set {_strPostcode = value; }
}
#endregion
}
And I use this class as follows:
-- Default.aspx.cs
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person("Mick", "Walker", 27, "xxxxR");
Session["SomePerson"] = myPerson;
Response.Redirect("Default2.aspx");
}
}
And in Default2.aspx
using System;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Person myPerson = new Person();
myPerson = (Person)Session["SomePerson"];
Response.Write(myPerson.strFName + "<br/>");
Response.Write(myPerson.strSName + "<br/>");
Response.Write(myPerson.intAge.ToString() + "<br/>");
Response.Write(myPerson.strPostcode + "<br/>");
}
}
This gives me the Error:
Error 1 Cannot implicitly convert type 'object' to 'Person'. An explicit
conversion exists (are you missing a cast?)
C:\Projects\StateTest\Default2.aspx.cs 17 20 C:\Projects\StateTest\
I am using SQL Server as my session provider if that makes a difference
(I did however mark the Person Object as Serializable).
Does anyone know what I am doing wrong?