0

So I have this method:

public IList<IndicationProject> GetProjectsForSponsor(int sponsorId)
        {
            IList<IndicationProject> result = new List<IndicationProject>();

            IWspWB_IndicationGetProjectsForEntityResultSet tmpResultSet = ExecWspWB_IndicationGetProjectsForEntity(sponsorId);

            if (tmpResultSet.WspWB_IndicationGetProjectsForEntity1 != null)
            {
                foreach (WspWB_IndicationGetProjectsForEntity1LightDataObject ldo in tmpResultSet.WspWB_IndicationGetProjectsForEntity1)
                {
                    result.Add(
                        new IndicationProject()
                            .Named(NullConvert.From(ldo.Stp_name, null))
                            .IdentifiedBy(NullConvert.From(ldo.Stp_straight_through_processing_id, 0))
                        );
                }
            }

            return result;
        }

Contained within this class:

namespace Web.Data.Indications
{
    public partial class IndicationsDataTier
    {

I want to use that method in another one of my classes, like so:

IList<IndicationProject> oldList = Web.Data.Indications.IndicationsDataTier.GetProjectsForSponsor(entityId);

But when I compile I get this error:

Error 44 An object reference is required for the non-static field, method, or property 'Web.Data.Indications.IndicationsDataTier.GetProjectsForSponsor(int)'

8 Answers 8

3

Your method is an instance member but you are calling it as if it were static.

If you want it to be a static method you should add the static keyword:

public static IList<IndicationProject> GetProjectsForSponsor(int sponsorId)
{
     // ...
}

If you want it to be an instance method you should create (or otherwise obtain a reference to) an instance of the type and then call the method on that instance:

using Web.Data.Indications;

// ...

IndicationsDataTier idt = new IndicationsDataTier();
IList<IndicationProject> oldList = idt.GetProjectsForSponsor(entityId);
Sign up to request clarification or add additional context in comments.

Comments

0

The method is not static so you need to actually instantiate the IndicationsDataTier

Comments

0

You are trying to access the method via the class, but you need to access it via an instance of the class, since it's not a static method.

Comments

0

The method header for the specified method does not include the static keyword.

Comments

0

You have declared the method non-static.

You are accessing it as a class method (MyClass.MyMethod), instead of as an instance method (myVar.MyMethod).

Change the declaration to be static to call it the way you are.

Comments

0

You have not declared the method as static, so you need to create an instance first. e.g.:

var oldList = new Web.Data.Indications.IndicationsDataTier();

oldList.GetProjectsForSponsor(int sponsorId);

Comments

0

From reading you method it looks like you can mark the methods as static.

Comments

0

If the constructor of IndicationsDataTier is parameterless you can try:

IList<IndicationProject> oldList = (new Web.Data.Indications.IndicationsDataTier).GetProjectsForSponsor(entityId);

or go with the static modifier..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.