Solved

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

Posted on 2013-01-07
6
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 75

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 75

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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 

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 75

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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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