简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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