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

发布于 2024-11-01 09:07:33 字数 1612 浏览 2 评论 0原文

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); < -- Intellisense 没有看到该方法。我得到“非静态字段、方法或属性需要对象引用” 'GetUsrGrps.GetGroups(用户组)' ???

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); < -- Intellisense does not see the Method. I get 'An object reference is required for the nonstatic field, method or property
'GetUsrGrps.GetGroups(UserGroup)' ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

菩提树下叶撕阳。 2024-11-08 09:07:33

GetGroups 是一个实例方法。使用static 关键字对其进行标记,或者创建其包含类的实例并引用该实例中的方法。考虑到方法 GetGroups 是接口的一部分,我建议采用实例路由,以便您的类定义仍然与接口约定匹配。

GetGroups is an instance method. Mark it with the static keyword or create an instance of its containing class and reference the method from that instance. 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.

鸵鸟症 2024-11-08 09:07:33

您收到该错误是因为 GetGroups 不是 GetUsrGrps 上的静态方法。

您需要将其声明为静态或创建 GetUsrGrps 的新实例来调用 GetGroups 方法。

You're getting that error because GetGroups is not a static method on GetUsrGrps.

You would either need to declare it as static or create a new instance of GetUsrGrps to call the GetGroups method.

荒岛晴空 2024-11-08 09:07:33

最好的选择是放弃 IGroups 接口,并将该方法标记为静态。由于这只是一个数据库访问对象,因此将其设置为静态方法是有意义的。

如果你想保留接口,那么你必须实例化一个对象。

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

Your best option is to abandon the IGroups interface, and mark the method as static. 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);
如果没有你 2024-11-08 09:07:33

它应该只是

usrGrp.UserGroups = GetGroups(usrGrp);

编译器认为您正在尝试调用静态方法,而 GetGroups 不是

It should be just

usrGrp.UserGroups = GetGroups(usrGrp);

The compiler thinks you're trying to call a static method, which GetGroups is not

烟酉 2024-11-08 09:07:33

您希望

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

您当前正在将其视为静态的,但似乎您的意图是通过 IGroups 接口访问它。

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.

救赎№ 2024-11-08 09:07:33

您的方法不是静态的,或者您的类未实例化。

您需要创建该类的一个实例,例如:

var getUsrGrps = new GetUsrGrps();

然后更改

usrGrp.UserGroups = GetUsrGrps.GetGroups(usrGrp);

usrGrp.UserGroups = getUsrGrps.GetGroups(usrGrp);

OR

更改

public IList<Group> GetGroups(UserGroup usrGroup)

public static IList<Group> GetGroups(UserGroup usrGroup)

看起来两者都适合您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文