Search code examples
c#classinterfaceilist

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)' ???


Solution

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