开发者

C# Interface Class Question Cannot See Method

开发者 https://www.devze.com 2023-02-26 05:49 出处:网络
public interface IGroups { IList<Group> GetGroups(UserGroup usrGrp); } public class GetUsrGrps : IGroups
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(gr开发者_如何学Cp);
            }
        }
        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)' ???


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.


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.


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


It should be just

usrGrp.UserGroups = GetGroups(usrGrp);

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号