Property missing

T

tshad

I pulled this from my last question to hopefully make it more clear.

After fixing my namespace problem I still need to find out why I am getting
an error message:

PageInit.cs(43,76): error CS0117: 'object' does not contain a definition for
'LastPageVisited'

User is an object I have in my session variables that works fine in VB.Net
but there is something missing here.

The Compiler Code is:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\csc /t:library PageInit.cs
/r:system.web.dll /r:system.data.dll /r:system.dll
/r:Microsoft.VisualBasic.dll /r:refresh.dll /r:User.dll

The code snippet giving me the problem looks like (the last line is the
offender):
************************************
using System;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Microsoft.VisualBasic;
using MyFunctions;

namespace MyFunctions
{
public class PageInit
{
public static void PageSetup(MyFunctions.Page thePage)
{
HttpContext.Current.Session["LastPageVisited"] =
(User)HttpContext.Current.Session["User"].LastPageVisited;
***********************************

My User class looks something like:
********************************
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using MyFunctions;
using FtsData;

namespace RolesBasedAuthentication
{
[Serializable]
public class User
{
private string lastPageVisited = "";
....
public string LastPageVisited
{
get { return lastPageVisited; }
set { lastPageVisited = value; }
}
....
*************************************

This is the piece that is giving me the problem:

(User)HttpContext.Current.Session["User"].LastPageVisited

As you can see, LastPageVisited is a property of User.

I am still getting this error, even if I change:

HttpContext.Current.Session["LastPageVisited"] =
(User)HttpContext.Current.Session["User"].LastPageVisited;

to

HttpContext.Current.Session["LastPageVisited"] =
(User)(HttpContext.Current.Session["User"]).LastPageVisited;

or

HttpContext.Current.Session["LastPageVisited"] =
(RolesBasedAuthentication.User)(HttpContext.Current.Session["User"]).LastPageVisited;

But the Class User does have a LastPageVisited:

public string LastPageVisited
{
get { return lastPageVisited; }
set { lastPageVisited = value; }
}

Am I casting it wrong??

Thanks,

Tom
 
M

Marc Gravell

The cast is in the wrong place; this attempts to cast
HttpContext.Current.Session["User"].LastPageVisited to (User) - you
mean:
HttpContext.Current.Session["LastPageVisited"] =
((User)HttpContext.Current.Session["User"]).LastPageVisited;

i.e. cast HttpContext.Current.Session["User"] to User, and then access
..LastPageVisited

Marc
 
J

Jon Skeet [C# MVP]

I pulled this from my last question to hopefully make it more clear.

<snip>

It's best not to start multiple threads like this - I've just answered
another post having checked it wasn't answered within the thread, and
now I find Marc's answered it here.

Jon
 
T

tshad

Marc Gravell said:
The cast is in the wrong place; this attempts to cast
HttpContext.Current.Session["User"].LastPageVisited to (User) - you mean:
HttpContext.Current.Session["LastPageVisited"] =
((User)HttpContext.Current.Session["User"]).LastPageVisited;

i.e. cast HttpContext.Current.Session["User"] to User, and then access
.LastPageVisited

That was it.

Thanks,

Tom
 
T

tshad

Jon Skeet said:
<snip>

It's best not to start multiple threads like this - I've just answered
another post having checked it wasn't answered within the thread, and
now I find Marc's answered it here.

I agree.

I only did it because I was afraid that it would get lost in answering the
first error message and I was in a big hurry.

Sorry.

Thanks,

Tom
 

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