方法“添加”没有重载需要2个参数 [英] No overload for method 'Add' takes 2 arguments

查看:239
本文介绍了方法“添加”没有重载需要2个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 ConnectionManager cm =  new  ConnectionManager(); 
cm.Command.CommandText = select * from tblFeedsItems,其中ItemID =' +( (按钮)发送者).CommandArgument + ';
cm.Command.CommandType = CommandType.Text;
cm.Connection.Open();
SqlDataReader dr = cm.Command.ExecuteReader();
while (dr.Read())
{
ListBox1.Items.Add( 333,(dr [ 标题]的ToString()))。
}
cm.Connection.Close();





重载错误符合:ListBox1.Items.Add(333,(dr [Title ] .ToString()));



为什么????

解决方案

没有过载这需要两个项目:只是表示要添加的对象的一个​​参数。

如果要添加两个对象,请考虑调用Add两次,或使用AddRange重载之一。

如果您尝试添加具有两个属性的对象,则需要创建一个容器类,并添加一个实例:

 < span class =code-keyword> public   class  MyContainer 
{
public string 编号{ get ; set ; }
public string 标题{ get ; set ; }
public MyContainer( string number, string 标题)
{
Number = number;
Title = Title;
}
}
...
ListBox1.Items.Add( new MyContainer( 333,(dr [ Title]。ToString())));

您可能还希望在容器类中重写ToString,以提供合理的人类可读输出。


ConnectionManager cm = new ConnectionManager();
           cm.Command.CommandText = "select * from tblFeedsItems where ItemID = '" + ((Button)sender).CommandArgument + "'";
           cm.Command.CommandType = CommandType.Text;
           cm.Connection.Open();
           SqlDataReader dr = cm.Command.ExecuteReader();
           while (dr.Read())
           {
               ListBox1.Items.Add("333", (dr["Title"].ToString()));
           }
           cm.Connection.Close();



the overload error is in line : ListBox1.Items.Add("333", (dr["Title"].ToString()));

why ????

解决方案

There is no overload that takes two items: just the one parameter representing the object to add.
If you want to add two objects, then consider calling Add twice, or using one of the AddRange overloads.
If you are trying to add an object with two properties, then you will need to create a container class, and add an instance of that instead:

public class MyContainer
   {
   public string Number { get; set; }
   public string Title { get; set; }
   public MyContainer(string number, string title)
      {
      Number = number;
      Title = Title;
      }
   }
...
                ListBox1.Items.Add(new MyContainer("333", (dr["Title"].ToString())));

You will probably also want to override ToString in the container class to give a "sensible" human readable output.


这篇关于方法“添加”没有重载需要2个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆