简体   繁体   中英

An object reference is required for the non-static field, method, or property

I read through the the other topics on the same issue, but not sure that i should work off an inctance, in my instance.

i have the following:

public interface ITrainingService : IDependency
{
   void ResetModule(int id, int userScormModuleId, int currentUser);
}

public class TrainingService : ITrainingService
{
 public void ResetModule(int id, int userScormModuleId, int currentUser)
 {
   Zinc.Repositories.Scorm.IScormModuleRepository.ResetModule(id, userScormModuleId, currentUser);  //i get the error here
 }
}

ResetModule is contained in:

public class ScormModuleRepository : Repository<ScormModule>, IScormModuleRepository
{
   public void ResetModule(int id, int userScormModuleId, int currentUser)
   {
     using (SqlConnection conn = new SqlConnection(ZincModelContainer.CONNECTIONSTRING))
     {
         using (SqlCommand cmd = conn.CreateCommand())
         {
           conn.Open();
           cmd.CommandType = System.Data.CommandType.StoredProcedure;
           cmd.CommandText = "[Zinc].[ResetUserScormModuleData]";

           SqlParameter param = new SqlParameter("@userId", System.Data.SqlDbType.Int);
           param.Value = id;
           cmd.Parameters.Add(param);

           SqlParameter param2 = new SqlParameter("@userScormModuleId", System.Data.SqlDbType.Int);
           param2.Value = userScormModuleId;
           cmd.Parameters.Add(param2);

           SqlParameter param3 = new SqlParameter("@currentUser", System.Data.SqlDbType.Int);
           param3.Value = currentUser;
           cmd.Parameters.Add(param3);

           cmd.ExecuteNonQuery();
        }
     }
  }
}
 public interface IScormModuleRepository : IRepository<ScormModule>
 {
   void ResetModule(int id, int userScormModuleId, int currentUser);
 }

the whole error: Error 1 An object reference is required for the non-static field, method, or property 'Zinc.Repositories.Scorm.IScormModuleRepository.ResetModule(int, int, int)' C:\\TFSPreview\\Zinc\\Project\\ServiceImplementations\\TrainingService.cs 501 8 Zinc

i dont understand? how must i work from an instance? or what must i instantiate or is my problem of another nature?

thanks

You need an instance of a class implementing IScormModuleRepository . With your code, what method should be called? IScormModuleRepository is an interface, it doesn't even contain an implementation of the method you are trying to call...

You need something like this:

public class TrainingService : ITrainingService
{
    IScormModuleRepository _repository;

    public TrainingService(IScormModuleRepository repository)
    {
        _repository = repository;
    }

    public void ResetModule(int id, int userScormModuleId, int currentUser)
    {
        _repository.ResetModule(id, userScormModuleId, currentUser);
    }
}

You now can use your TrainingService like this:

var repository = new ScormModuleRepository(...);
var trainingService = new TrainingService(repository);
trainingService.ResetModule(...);

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