-3

Can anyone tell me how do I populate my fields in my interface IAccount? I'm having an error in x.Add(new IAccount ...

public class IPersonRepo : IAccount
{
    string connectionstring = @"Server=SLI002/SQLEXPRESS;Database=atengturonDB;Trusted_Connection=true;";
    public int AccountsID
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountUserName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountPassword
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountSalt
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public void getAccount()
    {
        SqlConnection conn = new SqlConnection(connectionstring);
        using (SqlCommand comm = new SqlCommand())
        {
            List<IAccount> x = new List<IAccount>();
            comm.Connection = conn;
            comm.CommandText = "Select AccountsID,AccountsUserName,AccountsPassword,AccountsSalt from Accounts";
            comm.CommandType = CommandType.Text;
            SqlDataReader reader = null;
            conn.Open();
            comm.ExecuteNonQuery();
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                x.Add(new IAccount
              {
                  AccountsID = (int)reader["AccountsID"],
                  AccountUserName = (byte[])reader["AccountsUserName"],
                  AccountPassword = (byte[])reader["AccountsPassword"],
                  AccountSalt = (byte[])reader["AccountsSalt"]
              });

            }
            conn.Close();
        }
    }
}
Orifjon
  • 915
  • 8
  • 25
RE0824C
  • 73
  • 1
  • 3
  • 12

2 Answers2

0

Please, rename IPersonRepo to PersonRepo, prefix I means interface, but clearly it is class. Second, it doesnt look like repo (=repository) but like Person (but this is debatable... :) )

Third, you are trying to create interface - but you have to instance class which implements that interface:

//x.Add(new IAccount
//x.Add(new IPersonRepo
//x.Add(new PersonRepo
x.Add(new Person
          {
              AccountsID = (int)reader["AccountsID"],
              AccountUserName = (byte[])reader["AccountsUserName"],
              AccountPassword = (byte[])reader["AccountsPassword"],
              AccountSalt = (byte[])reader["AccountsSalt"]
          });

Fourth and last, maybe you should take a look at any ORM, like NHibernate or Entity Framework. Can help you, but its your call :)

Jan 'splite' K.
  • 1,667
  • 2
  • 27
  • 34
0

First, never use the I prefix when deciding on a class name. It's not a compilation error but it's very confusing since the convention is to use I as a prefix to interface names.
So your class should be called PersonRepo and not IPersonRepo.
(you can, however, name a class that starts with I like (Ice), just don't use I as a prefix)

Second, you can't instantiate an interface. you can use a variable of the interface type but instantiate the actual class: IAccount MyAccount = new PersonRepo();

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121