?
Solved

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

Posted on 2013-01-07
6
Medium Priority
?
431 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 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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…
Suggested Courses

801 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