简体   繁体   English

C#接口类问题看不到方法

[英]C# Interface Class Question Cannot See Method

public interface IGroups
{
    IList<Group> GetGroups(UserGroup usrGrp);
}

public class GetUsrGrps : IGroups
{
    public IList<Group> GetGroups(UserGroup usrGroup)
    {
        List<Group> grps = new List<Group>();
        UserGroupDao UsrGrpDao = new UserGroupDao();
        DbDataReader ddr = UsrGrpDao.GetUserGroups(usrGroup);
        if (ddr.HasRows)
        {
            while (ddr.Read())
            {
                Group grp = new Group();
                grp.GroupId = Convert.ToInt32(ddr["groupId"]);
                grps.Add(grp);
            }
        }
        else
        {
            Group grp = new Group();
            grp.GroupId = Convert.ToInt32("0");
            grps.Add(grp);
        }
        return grps;
    }
}



 public UserGroup GetUser(UserGroup usrGrp)
    {

        UserGroupDao usrGroupDao = new UserGroupDao();
        DbDataReader ddr = usrGroupDao.GetUser(usrGrp);
        if (ddr.HasRows)
        {
            while (ddr.Read())
            {
                usrGrp.Id = Convert.ToInt32(ddr["id"]);
                usrGrp.FirstName = Convert.ToString(ddr["firstname"]);
                usrGrp.LastName = Convert.ToString(ddr["lastname"]);
                usrGrp.UserName = Convert.ToString(ddr["username"]);
            }
        }
        usrGrp.UserGroups = GetUsrGrps.GetGroups(usrGrp);
        return this;
    }

usrGrp.Groups is defined as IList<Group>...?  }

**usrGrp.UserGroups = GetUsrGrps.GetGroups(usrGrp); ** usrGrp.UserGroups = GetUsrGrps.GetGroups(usrGrp); < -- Intellisense does not see the Method. < - Intellisense没有看到方法。 I get 'An object reference is required for the nonstatic field, method or property 'GetUsrGrps.GetGroups(UserGroup)' ??? 我得到'非静态字段,方法或属性'GetUsrGrps.GetGroups(UserGroup)'需要对象引用???

GetGroups is an instance method. GetGroups是一个实例方法。 Mark it with the static keyword or create an instance of its containing class and reference the method from that instance. 使用static关键字标记它或创建其包含类的实例并从该实例引用该方法。 Considering that the method, GetGroups is part of the interface, I would recommend going the instance route so that your class definition still matches the interface contract. 考虑到方法, GetGroups是接口的一部分,我建议去实例路由,以便你的类定义仍然匹配接口契约。

You're getting that error because GetGroups is not a static method on GetUsrGrps. 您收到该错误,因为GetGroups不是GetUsrGrps上的静态方法。

You would either need to declare it as static or create a new instance of GetUsrGrps to call the GetGroups method. 您可能需要将其声明为静态或创建GetUsrGrps的新实例以调用GetGroups方法。

Your method isn't static or your class is not instantiated. 您的方法不是静态的,或者您的类未实例化。

You need to create an instance of the class like: 您需要创建类的实例,如:

var getUsrGrps = new GetUsrGrps();

then change 然后改变

usrGrp.UserGroups = GetUsrGrps.GetGroups(usrGrp);

to

usrGrp.UserGroups = getUsrGrps.GetGroups(usrGrp);

OR 要么

change 更改

public IList<Group> GetGroups(UserGroup usrGroup)

to

public static IList<Group> GetGroups(UserGroup usrGroup)

It looks like either will work for you. 看起来任何一个都适合你。

It should be just 它应该是公正的

usrGrp.UserGroups = GetGroups(usrGrp);

The compiler thinks you're trying to call a static method, which GetGroups is not 编译器认为您正在尝试调用静态方法,而GetGroups则不是

You want 你要

 IGroups groups = new GetUsrGrps();
 usrGrp.UserGroups = groups.GetGroups(usrGrp);

You are currently calling it as if it were static, but it seems that your intention is to access it through the IGroups interface. 您当前正在调用它,就像它是静态的一样,但您的意图似乎是通过IGroups接口访问它。

Your best option is to abandon the IGroups interface, and mark the method as static. 您最好的选择是放弃IGroups接口,并将该方法标记为静态。 Since this is just a database access object anyway, it makes sense for this to be a static method. 由于这只是一个数据库访问对象,因此将其作为静态方法是有意义的。

If you want to keep the interface, then you will have to instantiate an object. 如果要保留接口,则必须实例化对象。

usrGrp.UserGroups = (new GetUsrGrps).GetGroups(usrGrp);

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

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