使用添加列表有问题

时间:2014-12-13 16:22:00

标签: c# list class add

更新#1: 这些是我所做的改变,但它们都不起作用:

1)删除静态

string date;
string title;
string description;
string organizer;
string place;

2)制作当地的

events tempe = new events();
tempe.setDate(event_info[0]);
tempe.setTitle(event_info[1]);
tempe.setPlace(event_info[2]);
tempe.setDesc(event_info[3]);
tempe.setOrganizer(event_info[4]);
eventsarray.Add(tempe);
tempe = null;

3)直接使用列表

eventsarray.Add(new events());
eventsarray[eventsarray.Count - 1].setDate(event_info[0]);
eventsarray[eventsarray.Count - 1].setTitle(event_info[1]);
eventsarray[eventsarray.Count - 1].setPlace(event_info[2]);
eventsarray[eventsarray.Count - 1].setDesc(event_info[3]);
eventsarray[eventsarray.Count - 1].setOrganizer(event_info[4]);

原始问题

我正在尝试将事件列表存储到列表中,但是当我尝试添加新事件时,列表中所有其他事件实例也被最后一个输入替换。 下面的代码包含使用.Add将新事件存储到列表中的过程,由3组代码包围,提供输出以进行调试。

if (eventsarray.Count > 0)
{
    richTextBox1.Text = richTextBox1.Text + "Before Insert: \r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray.Count: " + eventsarray.Count.ToString() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getDate: " + eventsarray[0].getDate() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getTitle: " + eventsarray[0].getTitle() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getPlace: " + eventsarray[0].getPlace() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getDesc: " + eventsarray[0].getDesc() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getOrganizer: " + eventsarray[0].getOrganizer() + "\r\n\r\n";
}


//store all into into a new event in eventsarray

tempe.setDate(event_info[0]);
tempe.setTitle(event_info[1]);
tempe.setPlace(event_info[2]);
tempe.setDesc(event_info[3]);
tempe.setOrganizer(event_info[4]);
//something is wrong with .Add?
eventsarray.Add(tempe);


if (eventsarray.Count > 0)
{
    richTextBox1.Text = richTextBox1.Text + "After Insert: \r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray.Count: " + eventsarray.Count.ToString() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getDate: " + eventsarray[0].getDate() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getTitle: " + eventsarray[0].getTitle() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getPlace: " + eventsarray[0].getPlace() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getDesc: " + eventsarray[0].getDesc() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[0].getOrganizer: " + eventsarray[0].getOrganizer() + "\r\n\r\n";
}
richTextBox1.Text = richTextBox1.Text + "eventsarray.Count: " + eventsarray.Count.ToString() + "\r\n";
richTextBox1.Text = richTextBox1.Text + "Event " + event_info[1] + " has been added to List." + "\r\n\r\n";

for (int i = 0; i < eventsarray.Count; i++)
{
    richTextBox1.Text = richTextBox1.Text + "Looping: "+ i.ToString()  + "\r\n" ;
    richTextBox1.Text = richTextBox1.Text + "eventsarray.Count: " + eventsarray.Count.ToString() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[i].getDate: " + eventsarray[i].getDate() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[i].getTitle: " + eventsarray[i].getTitle() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[i].getPlace: " + eventsarray[i].getPlace() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[i].getDesc: " + eventsarray[i].getDesc() + "\r\n";
    richTextBox1.Text = richTextBox1.Text + "eventsarray[i].getOrganizer: " + eventsarray[i].getOrganizer() + "\r\n\r\n";
}

并且输出是这样的(第一个事件和第二个事件的输入是不同的):

Output of the for loop

在.Add的代码之前,它仍然完美无缺。我尝试使用断点来查看是否存在任何循环而不是使该行运行两次,但它只运行一次。我尝试使用.Insert(0,eventsarray),但问题仍然存在,输出完全相同。有谁有想法吗?感谢。

1 个答案:

答案 0 :(得分:1)

如果变量tempe是一次初始化的全局变量(可能在添加之前的某个点),则可以解释该问题

如果是这种情况,当您更改tempe变量的值时,您将更改上一个Add中引用的同一对象。更改值不会创建另一个tempe类型的实例。当您第二次读取变量tempe时,您的数组中有两个项目,但它们都使用上次更新中应用的值引用内存中的同一对象

要修复你只需要:

// This creates a new instance of an Events 
Events tempe = new Events();

tempe.setDate(event_info[0]);
tempe.setTitle(event_info[1]);
tempe.setPlace(event_info[2]);
tempe.setDesc(event_info[3]);
tempe.setOrganizer(event_info[4]);
eventsarray.Add(tempe);

此处的一部分还有另一个错误。代码tempe.setDate(event_info[0]);似乎设置了为类Events定义的STATIC变量date。静态变量在类的所有实例之间共享。因此,即使您创建了一个新的局部变量Events,也可以将所有类型为Events的变量的STATIC日期结果设置为具有相同的值。

您需要将这些静态变量更改为实例成员。

public class Events
{
   private string date;
   private string title;
   private string description;
   private string organizer;
   private string place;

   public void setDate(string newValue)
   {
        date = newValue;
   }
   .... other set methods....
}

或更改所有内容并将公共属性与getter和setter访问器一起使用

public class Events
{
   public string Date {get; set;}
   public string Title {get; set;}
   public string Description {get; set;}
   public string Organizer {get; set;}
   public string Place {get; set;}
}

....
// This creates a new instance of an Events 
Events tempe = new Events();
tempe.Date = event_info[0];
.....