Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-07
6
Medium Priority
?
435 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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