简体   繁体   中英

Using a method from a partial class

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)'

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

您尝试通过类访问该方法,但是您需要通过该类的实例访问它,因为它不是静态方法。

指定方法的方法标头不包含static关键字。

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.

该方法不是静态的,因此您需要实际实例化IndicationsDataTier

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

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

oldList.GetProjectsForSponsor(int sponsorId);

通过阅读您的方法,看起来您可以将这些方法标记为静态。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM