【问题标题】:Use the 'new' keyword使用“新”关键字
【发布时间】:2015-09-28 05:47:55
【问题描述】:

我正在尝试使用 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; }
}

在我的另一个类(DAL)中,我有一个方法,这个方法需要在 Country 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;

                }

            }

        }

当我这样做时,会向我显示这样的错误:“使用 'new' 关键字来创建对象实例” 我该如何解决?

【问题讨论】:

  • 在哪一行出现错误?
  • 这里: if (dr["NameCountry"] != DBNull.Value) obj.Country.NameCountry = dr["NameCountry"].ToString();
  • 出现错误时。错误主要是因为你试图创建一些没有新关键字的类的实例。
  • 你能发布完整的堆栈跟踪吗?您是否从存储过程中获取所有值??
  • 在程序中返回给我“NameCountry”。我需要把新的关键字,但我不知道在哪里! ://

标签: c# asp.net .net model ado.net


【解决方案1】:

您必须在初始化 NameCountry 之前创建 Country 类的对象

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

【讨论】:

    【解决方案2】:

    您已经创建了Banner 类的对象,但尚未创建包含在Country 类横幅中的对象。因此,在使用它之前先创建它。您可以在 Banner 类的默认构造函数中执行此操作。

    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 中的对象来扩展它。

    【讨论】:

    • 虽然这看起来很干净,但有时横幅没有与之关联的国家/地区(例如表列 Banner.IdCountry 在 SQL 中被允许接受 NULL)。这仍然会给出一个有效的国家,而不是一个不存在的国家来表示数据库中的 NULL。
    • 我使用了你的解决方案!谢了!!
    • @vnikhil 谢谢,我指出了错误的可能原因,并提出了消除错误的方法,以保持简单和问题的重点。我通过建议 OP 根据要求进行调整来更新答案。
    【解决方案3】:

    在 Banner 类中 Country 是 Country 类的类属性。因此,如果您首先在此属性中添加值,则需要为该类创建对象。

    添加以下行:-

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

    【讨论】:

      猜你喜欢
      • 2017-11-25
      • 2012-05-06
      • 1970-01-01
      • 2017-05-31
      • 2012-10-28
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多