for (int i = 0; i < listBox1.Items.Count; i++)
            {
                //循环获取选中字段的英文名
                string   str=(this.listBox1.Items[i]  as   MyItemlist).Value;
                //循环获取选中字段的中文名
                string titleName = (this.listBox1.Items[i] as MyItemlist).Text;
                cName += titleName + ",";
                eName += str+",";
            }
string   str=(this.listBox1.Items[i]  as   MyItemlist).Value;
这句话报错,没有被实例化是什么原因啊?,谢谢了。

解决方案 »

  1.   

    说明this.listBox1.Items[i]转为MyItemlist对象的时候失败了,返回了一个null
      

  2.   

    我猜你这个MyItemlist应该是List<MyItem>,因此:
    string str=(this.listBox1.Items[i] as MyItem).Value;
    string titleName = (this.listBox1.Items[i] as MyItem).Text;如果还不行,检查listBox1的Items.Add或DataSource绑定数据的相关代码
      

  3.   

           private void moveUpListBox(ListBox ListBox1) //向上移动
            {
                //若不是第一行则上移
                if (ListBox1.SelectedIndex > 0)
                {
                    int index = ListBox1.SelectedIndex;
                    string temp = ListBox1.Items[index - 1].ToString();
                    ListBox1.Items[index - 1] = ListBox1.SelectedItem.ToString();
                    ListBox1.Items[index] = temp;
                    ListBox1.SelectedIndex = index - 1;
                }
            }
    为什么只要调用这个方法string str=(this.listBox1.Items[i] as MyItemlist).Value;
    就说没有被实例化了?急啊,新手求大家帮忙了
      

  4.   

    你把你的MyItemlist贴出来让别人帮你分析分析
      

  5.   

    string str=(this.listBox1.Items[i] as MyItemlist).Value;
    这句话不对吧
      

  6.   

            private void moveUpListBox(ListBox ListBox1) //向上移动
            {
                //若不是第一行则上移
                if (ListBox1.SelectedIndex > 0)
                {
                    int index = ListBox1.SelectedIndex;
                    string temp = ListBox1.Items[index - 1].ToString();
                    ListBox1.Items[index - 1] = ListBox1.SelectedItem.ToString();
                    ListBox1.Items[index] = temp;
                    ListBox1.SelectedIndex = index - 1;
                }
            }
    为什么调用这个方法就取不到值
               int lbxLength = this.listBox1.Items.Count;//listbox的长度   
                int iselect = this.listBox1.SelectedIndex;//listbox选择的索引   
                if (lbxLength > iselect && iselect > 0)
                {
                    object oTempItem = this.listBox1.SelectedItem;
                    this.listBox1.Items.RemoveAt(iselect);
                    this.listBox1.Items.Insert(iselect - 1, oTempItem);
                    this.listBox1.SelectedIndex = iselect - 1;
                } 
    这个就能用?大家能帮我分析下嘛?
      

  7.   

    你问题问的不清楚,大家怎么帮你分析?第一,看不到你在哪里调用了moveUpListBox,第二,MyItemlist也不知是啥……总之,问问题请把问题描述清楚。PS,我只是来提醒你问问题要问清楚的。
      

  8.   

    例如这句:ListBox1.Items[index - 1] = ListBox1.SelectedItem.ToString();
    使用的ToString后,Items[index - 1]不就成引用了个String对象了吗?
    然后你再想Items[index - 1] as MyItemlist当然为null了,因为已经不是MyItemlist对象了
      

  9.   

    string str=(this.listBox1.Items[i] as MyItemlist).Value;首先 看一下this.listBox1.Items[i]这里面是否有值 然后看一下 this.listBox1.Items[i] 返回值类型是否和 MyItemlist相同么
      

  10.   

    +1
    你在for循环中把对象从MyItemlist变成了string类型了,需要改为:
    int index = ListBox1.SelectedIndex;
    object temp = ListBox1.Items[index - 1];
    ListBox1.Items[index - 1] = ListBox1.SelectedItem;
    ListBox1.Items[index] = temp;
    保留原来的类型