C# 接口类问题看不到方法
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
GetGroups
是一个实例方法。使用static
关键字对其进行标记,或者创建其包含类的实例并引用该实例中的方法。考虑到方法GetGroups
是接口的一部分,我建议采用实例路由,以便您的类定义仍然与接口约定匹配。GetGroups
is an instance method. Mark it with thestatic
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.您收到该错误是因为 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.
最好的选择是放弃 IGroups 接口,并将该方法标记为静态。由于这只是一个数据库访问对象,因此将其设置为静态方法是有意义的。
如果你想保留接口,那么你必须实例化一个对象。
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.
它应该只是
编译器认为您正在尝试调用静态方法,而 GetGroups 不是
It should be just
The compiler thinks you're trying to call a static method, which GetGroups is not
您希望
您当前正在将其视为静态的,但似乎您的意图是通过
IGroups
接口访问它。You want
You are currently calling it as if it were static, but it seems that your intention is to access it through the
IGroups
interface.您的方法不是静态的,或者您的类未实例化。
您需要创建该类的一个实例,例如:
然后更改
为
OR
更改
为
看起来两者都适合您。
Your method isn't static or your class is not instantiated.
You need to create an instance of the class like:
then change
to
OR
change
to
It looks like either will work for you.