Isabell
asked on
an object reference is required for the non-static field method or property
I have
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~
public class myPage: BasePage{
[FindsBy(How = How.XPath, Using = $"//img[@src='//{MyPath()}/img.png']")]
public IWebElement MyImg {get; set;}
}
public abstract class BasePage{
public string MyPath(){
......
return result.toString();
}
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~
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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.
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; }
}
}
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";
}
}
}
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>
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)
public abstract class BasePage{
public string MyPath(){
......
return result.toString(); <<------------------------
}
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
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"
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.
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"
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";
}
}
}
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}/im g.png']")]
should be OK. However I suspect you want to modify MyPath at runtime and that isn't going to compile.
public abstract class BasePage
{
public const string MyPath = "Test Value";
and
[FindsBy(How = How.XPath, Using = $"//img[@src='//{BasePage.
should be OK. However I suspect you want to modify MyPath at runtime and that isn't going to compile.