Solved

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

Posted on 2013-01-07
6
421 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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)…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

778 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