Link to home
Start Free TrialLog in
Avatar of Isabell
Isabell

asked on

an object reference is required for the non-static field method or property

I have
public class myPage: BasePage{
[FindsBy(How = How.XPath, Using = $"//img[@src='//{MyPath()}/img.png']")]
      public IWebElement MyImg {get; set;}

}

Open in new window


public abstract class BasePage{
     public string MyPath(){
          ......
          return result.toString();
}

Open in new window


inside myPage class, I used MyPath() inside the string interpolation.
And I am getting this error there.
an object reference is required for the non-static field method or property 'BasePage.MyPath()'

What's the reason that I am getting this error and how to fix it?

Thanks~
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Can you post more code, specifically just what your variable result is and how and where it is declared.  (I suspect it is at class level and not static - so the compiler needs an object instance otherwise it is just does not exist when you attempt to use it).
ASKER CERTIFIED SOLUTION
Avatar of Nikoloz Khelashvili
Nikoloz Khelashvili
Flag of Georgia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I found source, if it helps, AndyAinscow

FindsBy attribute source code
@Nikoloz
Yes, my comment is asking for more information to determine exactly where the problem is.  You suspect what I mentioned in my comment, trying to use a non static variable in a static function without any valid instance of the object containing the non static variable.
Yes, my comment is asking for more information to determine exactly where the problem is.  You suspect what I mentioned in my comment, trying to use a non static variable in a static function without any valid instance of the object containing the non static variable.

No, I dont suspect, I tested it. I commented the attribute mentioned above, and project build was successful. With attribute you get error

You can add following 2 classed to your project

using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticMethodTestProject
{
    public class myPage : BasePage
    {
        [FindsBy(How = How.XPath, Using = $"//img[@src='//{MyPath()}/img.png']")]
        public IWebElement MyImg { get; set; }
    }
}

Open in new window


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticMethodTestProject
{
    public abstract class BasePage
    {
        public string MyPath()
        {
            return "Test value";
        }
    }
}

Open in new window


and below is packages.config file I have:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Newtonsoft.Json" version="12.0.1" targetFramework="net472" />
  <package id="NSpectator.Selenium" version="0.9.9" targetFramework="net472" />
  <package id="Selenium.Support" version="3.141.0" targetFramework="net472" />
  <package id="Selenium.WebDriver" version="3.141.0" targetFramework="net472" />
  <package id="Selenium.WebDriver.StrongNamed" version="3.12.0" targetFramework="net472" />
</packages>

Open in new window

From the question
public abstract class BasePage{
     public string MyPath(){
          ......
          return result.toString();    <<-----------------------------------   Is this the cause of the error ?
}

an object reference is required for the non-static field method or property 'BasePage.MyPath()'

What's the reason that I am getting this error and how to fix it?



I'm suspicious of this line, hence my question just how is 'result' declared.  It might just require a simple extra keyword there to cure the problem.  (Removing the failing attribute my stop the error but probably isn't the cure wanted)
No, you are getting error even if MyPath() method returns constant result. It is not the case
Avatar of Isabell
Isabell

ASKER

Thanks guys.
I changed the method to static after I read your answers. So now MyPath() is a static method.

But then I am getting this error:

"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
Please post your complete code.
Avatar of Isabell

ASKER

Hi AndyAinscow,

Please look at Nikoloz Khelashvili's post above.
You can use it as a complete code. it's really the same as my complete code.
But this time, I made it "static"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StaticMethodTestProject
{
    public abstract class BasePage
    {
        public static string MyPath()
        {
            return "Test value";
        }
    }
}

Open in new window

OK.  The attribute has to have a constant supplied so

    public abstract class BasePage
    {
        public const string MyPath = "Test Value";


and
[FindsBy(How = How.XPath, Using = $"//img[@src='//{BasePage.MyPath}/img.png']")]

should be OK.  However I suspect you want to modify MyPath at runtime and that isn't going to compile.