使用 'new' 关键字

Use the 'new' keyword

我正在尝试使用 ADO 在实体之间建立关系。

public class Banner
{
    public int IdBanner { get; set; }
    public string NameBanner { get; set; }
    public string Media { get; set; }

    public Country Country{ get; set; }
}

public class Country 
{
    public int IdCountry { get; set; }
    public string NameCountry { get; set; }
}

在我的另一个 class (DAL) 中,我有一个方法,这个方法我需要在国家 Class 中插入一个属性,就像我的例子:

public List<Banner> Listar()
{
            List<Banner> lista = new List<Banner>();

            using (SqlConnection conn = ConnectionDAL.GetConnection())
            {
                String sql = "BannerListar";

                using (SqlCommand command = new SqlCommand(sql, conn))
                {

                    command.CommandType = CommandType.StoredProcedure;

                    try
                    {
                        conn.Open();

                        SqlDataReader dr = command.ExecuteReader();

                        while (dr.Read())
                        {
                            Banner obj = new Banner();

                            if (dr["IdBanner"] != DBNull.Value)
                                obj.IdBanner = Convert.ToInt32(dr["IdBanner"]);

                            if (dr["NameBanner"] != DBNull.Value)
                                obj.NameBanner = dr["NameBanner"].ToString();

                            if (dr["Media"] != DBNull.Value)
                                obj.Media = dr["Media"].ToString();


                //HERE the problem
                            if (dr["NameCountry"] != DBNull.Value)
                                obj.Country.NameCountry = dr["NameCountry"].ToString();



                            lista.Add(obj);

                        }
                    }
                    catch
                    {
                        throw;
                    }

                    finally
                    {
                        conn.Close();
                        conn.Dispose();
                    }


                    return lista;

                }

            }

        }

当我执行此操作时,会出现如下错误:"Use the 'new' keyword to create an object instance" 我该如何解决?

您必须在初始化 Name Country

之前创建 Country class 的对象
 if (dr["NameCountry"] != DBNull.Value)
   {
      Country country = new Country();
      country.NameCountry = dr["NameCountry"].ToString(); 
obj.Country = country;
}

在横幅中 class 国家 class 属性 代表国家 class。因此,如果您首先在此 属性 中添加值,则需要为该 class.

创建对象

添加下面一行:-

obj.Country = new Country(); 
if (dr["NameCountry"] != DBNull.Value)
   obj.Country.NameCountry = dr["NameCountry"].ToString();

您已经创建了 class Banner 的对象,但未创建 class Country 的横幅中包含的对象。所以在你使用它之前创建它。您可以在 Banner class.

的默认构造函数中执行此操作
public class Banner
{
    public Banner() //Add the default constructor and initiate the Country object
    {
        Country = new Country();
    }
    public int IdBanner { get; set; }
    public string NameBanner { get; set; }
    public string Media { get; set; }

    public Country Country{ get; set; }
}

我已经给出了可能出现问题的原因并提出了一个解决方案,该解决方案将以最小的努力消除错误,并有意保持简单以便更好地理解。您可以通过显式传递 Country 或传递 null 代替 Country 或创建 Country 对象并将其分配给 Banner.

中的对象来扩展它