Force a cast

J

Jeremy Chapman

I want to access the viewstate of a page object from outside of the class
(in a helper class), so I made a class inherited from Page, and exposed a
property that returns the viewstate object. I my helper class I cast the
page to my page class and access the exposed property, but the cast causes
an 'Specified cast is not valid' exception. I used to do this in delphi and
c++ all the time to access protected stuff, is it possible in .NET?
 
O

Oliver Wong

Jeremy Chapman said:
I want to access the viewstate of a page object from outside of the class
(in a helper class), so I made a class inherited from Page, and exposed a
property that returns the viewstate object. I my helper class I cast the
page to my page class and access the exposed property, but the cast causes
an 'Specified cast is not valid' exception. I used to do this in delphi
and c++ all the time to access protected stuff, is it possible in .NET?

Casting is possible in .NET. Post actual source code and it'll be easier
to help you.

- Oliver
 
K

Kevin Spencer

I think you THINK you used to so this in Delphi, but it is simply not
possible. Why? Because you can only cast a type as a type that it IS, that
is, the type that it is, or a type that it inherits. Since your custom class
inherits System.Web.UI.Page, you can cast IT as a System.Web.UI.Page class,
but you can not cast a System.Web.UI.Page class as your custom class.

However, be of good cheer. In fact, your solution is needlessly complex.
Remember that your Page is not a System.Web.UI.Page class, but a
"YourClassName" class, which inherits System.Web.UI.Page. IT has access to
its own ViewState, and can expose it via any method you choose.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Expect the unaccepted.
 
J

Jeremy Chapman

Here is some sample source.

using System;

namespace Mynamespace
{
/// <summary>
/// Helper functions for a page
/// </summary>
public class HelperClass
{
public HelperClass
{
//
// TODO: Add constructor logic here
//
}


private class CustomPage : System.Web.UI.Page
{
public CustomPage()
{
}

public System.Web.UI.StateBag PublishViewState
{
get
{
return ViewState;
}
}
}

public static void HelperTest(System.Web.UI.Page pPage)
{
CustomPage pPageCustom = (CustomPage)pPage;
pPageCustom.PublishViewState["test"] = DateTime.Now;

}
}
}
 
J

Jeremy Chapman

I definitely know this could be done in Delphi, it is a very common
technique.

Here is some sample code to help visualize better.

using System;

namespace Mynamespace
{
/// <summary>
/// Helper functions for a page
/// </summary>
public class HelperClass
{
public HelperClass
{
//
// TODO: Add constructor logic here
//
}


private class CustomPage : System.Web.UI.Page
{
public CustomPage()
{
}

public System.Web.UI.StateBag PublishViewState
{
get
{
return ViewState;
}
}
}

public static void HelperTest(System.Web.UI.Page pPage)
{
CustomPage pPageCustom = (CustomPage)pPage;
pPageCustom.PublishViewState["test"] = DateTime.Now;

}
}
}
 
O

Oliver Wong

Jeremy Chapman said:
Here is some sample source.

using System;

namespace Mynamespace
{
/// <summary>
/// Helper functions for a page
/// </summary>
public class HelperClass
{
public HelperClass
{
//
// TODO: Add constructor logic here
//
}


private class CustomPage : System.Web.UI.Page
{
public CustomPage()
{
}

public System.Web.UI.StateBag PublishViewState
{
get
{
return ViewState;
}
}
}

public static void HelperTest(System.Web.UI.Page pPage)
{
CustomPage pPageCustom = (CustomPage)pPage;
pPageCustom.PublishViewState["test"] = DateTime.Now;

}
}
}

This code looks fine except for the fact that PublishViewState is a
read-only property, and you're assigning to it.

What does the code invoking it look like?

- Oliver
 
J

Jeremy Chapman

I hadn't gotten to test the assignment because the cast failed first which I
haven't been able to solve.

Oliver Wong said:
Jeremy Chapman said:
Here is some sample source.

using System;

namespace Mynamespace
{
/// <summary>
/// Helper functions for a page
/// </summary>
public class HelperClass
{
public HelperClass
{
//
// TODO: Add constructor logic here
//
}


private class CustomPage : System.Web.UI.Page
{
public CustomPage()
{
}

public System.Web.UI.StateBag PublishViewState
{
get
{
return ViewState;
}
}
}

public static void HelperTest(System.Web.UI.Page pPage)
{
CustomPage pPageCustom = (CustomPage)pPage;
pPageCustom.PublishViewState["test"] = DateTime.Now;

}
}
}

This code looks fine except for the fact that PublishViewState is a
read-only property, and you're assigning to it.

What does the code invoking it look like?

- Oliver
 
O

Oliver Wong

Jeremy Chapman said:
I hadn't gotten to test the assignment because the cast failed first which
I haven't been able to solve.

Here's the code I typed on my machine and it compiles without errors. Either
you're using a different version of C# than I am, or there's some
information you haven't given me.

using System.Web.UI;
using System;

namespace Mynamespace {

class someClass {
class CustomPage : System.Web.UI.Page {

public System.Web.UI.StateBag PublishViewState {
get {
return ViewState;
}
}
}

public static void HelperTest(System.Web.UI.Page pPage) {
CustomPage pPageCustom = (CustomPage)pPage;
pPageCustom.PublishViewState["test"] = DateTime.Now;
}
}
}

- Oliver
 
J

Jeremy Chapman

It's not a compile time error, it's a runtime error when it tries to cast.

Oliver Wong said:
Jeremy Chapman said:
I hadn't gotten to test the assignment because the cast failed first which
I haven't been able to solve.

Here's the code I typed on my machine and it compiles without errors.
Either you're using a different version of C# than I am, or there's some
information you haven't given me.

using System.Web.UI;
using System;

namespace Mynamespace {

class someClass {
class CustomPage : System.Web.UI.Page {

public System.Web.UI.StateBag PublishViewState {
get {
return ViewState;
}
}
}

public static void HelperTest(System.Web.UI.Page pPage) {
CustomPage pPageCustom = (CustomPage)pPage;
pPageCustom.PublishViewState["test"] = DateTime.Now;
}
}
}

- Oliver
 
O

Oliver Wong

Jeremy Chapman said:
It's not a compile time error, it's a runtime error when it tries to cast.

The error is probably not in the code you posted, but in the page
invoking this code. Can you post that source as well?

- Oliver
 
J

jasonkester

Why not:

public static void HelperTest(CustomPage pPage)
{
pPage.PublishViewState["­test"] = DateTime.Now;
}

As in, why would you want to cast down to Page, then immediately cast
back up in your helper function? Your code would only work if you're
already a CustomPage, so why are you passing yourself as anything else?

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
J

Jeremy Chapman

Because the class being passed into HelperTest is actually Page, not
CustomPage. I created CustomPage as sort of a hack in an attempt to access
the viewstate externally.

Why not:

public static void HelperTest(CustomPage pPage)
{
pPage.PublishViewState["­test"] = DateTime.Now;
}

As in, why would you want to cast down to Page, then immediately cast
back up in your helper function? Your code would only work if you're
already a CustomPage, so why are you passing yourself as anything else?

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
J

jasonkester

In that case, you're going down the wrong path. It looks like you have
two options. Either derive your Page off of CustomPage and pass it to
your function, or simply pass the ViewState as a parameter (by
reference if you want to change it).

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

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