Solved

An object reference is required for the non-static field, method, or property Error

Posted on 2013-01-07
6
415 Views
Last Modified: 2013-01-07
I have a Master Page that contains a method for ErrorHandling.  In my pages that call the Master page Method I use the following code:

CVL pm = (CVL)Page.Master;
pm.ErrorHandling(....);

What I want to do is to pull the CVL pm = (CVL)Page.Master up and make it a global variable on the page so I don't have to create an instance of it in each module on the page.  When I try and do that I get the "An object reference is required for the non-static field, method, or property" error.  I realize it has to do with Page but I don't know how to resolve it to get what I want.  Any help is greatly appreciated!
0
Comment
Question by:dyarosh
  • 3
  • 3
6 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38752256
Which line raises the error?
0
 

Author Comment

by:dyarosh
ID: 38752369
namespace CVL
{
    public partial class CVLLookup : System.Web.UI.Page
    {
        CVL pm = (CVL)Page.Master;

        protected void Page_Load(object sender, EventArgs e)
        {

Open in new window

The line CVL pm=(CVL)Page.Master raises the following error:

An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Master.get'
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38752416
You need to move that initialization into one of the page methods. Where you have it causes it to run before the page is fully initialized. Try:

namespace CVL
{
    public partial class CVLLookup : System.Web.UI.Page
    {
        CVL pm;

        protected override OnInit(EventArgs e)        
        {
            pm = (CVL)Page.Master;
            
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dyarosh
ID: 38752476
I will try that.  I am having another problem when I try and build without making pm a global.  I'm going to post a separate question for that.  You may want to look for that question which will be titled: The type name 'CVL' does not exist in the type 'CVL.CVL'.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 38752490
Note that in the above "pm" is still global. You're just deferring initialization until further into the page life cycle.
0
 

Author Closing Comment

by:dyarosh
ID: 38752756
Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now