简体   繁体   English

使用部分类中的方法

[英]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)' 错误44非静态字段,方法或属性'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: 如果希望它是静态方法,则应添加static关键字:

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). 您将其作为类方法(MyClass.MyMethod)而不是实例方法(myVar.MyMethod)进行访问。

Change the declaration to be static to call it the way you are. 将声明更改为static以您自己的方式进行调用。

该方法不是静态的,因此您需要实际实例化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: 如果IndicationsDataTier的构造函数是无参数的,则可以尝试:

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

or go with the static modifier.. 使用static修饰符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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