Using a method from a partial class

4.3k Views Asked by At

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

There are 8 best solutions below

0
On BEST ANSWER

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

0
On

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.

0
On

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);
0
On

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

0
On

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.

0
On

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);
0
On

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

0
On

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..