首页 新闻 赞助 找找看

关于List泛型当做参数传递的问题

0
[已解决问题] 解决于 2014-10-30 21:25

首先声明一个Person类 三个字段 姓名,年龄,身高

 1     public class Person
 2     {
 3         private int age;
 4 
 5         public int Age
 6         {
 7             get { return age; }
 8             set { age = value; }
 9         }
10         private string name;
11 
12         public string Name
13         {
14             get { return name; }
15             set { name = value; }
16         }
17         private string height;
18 
19         public string Height
20         {
21             get { return height; }
22             set { height = value; }
23         }
24     }

添加WPF窗体
XAML添加一个DataGrid 和一个 按钮

cs中代码:

 1     public partial class MainWindow : Window
 2     {
 3         public MainWindow()
 4         {
 5             InitializeComponent();
 6             List<Person> list = new List<Person>();
 7             list.Add(new Person() { Age = 1, Name = "a", Height = "11" });
 8             list.Add(new Person() { Age = 3, Name = "b", Height = "12" });
 9             list.Add(new Person() { Age = 11, Name = "c", Height = "13" });
10             list.Add(new Person() { Age = 43, Name = "d", Height = "14" });
11             list.Add(new Person() { Age = 12, Name = "e", Height = "15" });
12             dataGrid1.ColumnWidth = 100;
13             dataGrid1.RowHeight = 30;
14             this.dataGrid1.ItemsSource = list;
15         }
16         private void button1_Click(object sender, RoutedEventArgs e)
17         {
18             List<Person> persons = this.dataGrid1.ItemsSource as List<Person>;
19             Window1 win = new Window1(persons);
20             win.ShowDialog();
21         }
22     }

点击按钮 将 persons泛型当做参数传递 给窗体 Window1构造函数

Window1窗体中有DataGrid绑定传过来的persons

 1     public partial class Window1 : Window
 2     {
 3         public Window1()
 4         {
 5             InitializeComponent();
 6         }
 7         public Window1(List<Person> list)
 8         {
 9             InitializeComponent();
10             List<Person> persons = new List<Person>();
11             foreach (var i in list)
12             {
13                 persons.Add(i);
14             }
15             foreach (var p in persons)
16             {
17                 p.Age = p.Age + 1;
18             }
19             dataGrid1.ColumnWidth = 100;
20             dataGrid1.RowHeight = 30;
21             this.dataGrid1.ItemsSource = persons;
22         }
23     }

我想让按钮弹出来的新窗体中DataGrid绑定的persons的年龄加1

问题来了: 初始年龄 1、3、11、43、12
第一次点击按钮

年龄 都加1 为 2、4、12、44、13

关闭窗体再次点击按钮为什么 年龄不是  2、4、12、44、13

而是又加了1??

xyz0的主页 xyz0 | 菜鸟二级 | 园豆:201
提问于:2014-10-30 16:39
< >
分享
最佳答案
0

你只是新建了LIST,里面的变量Person还是指向原来的Person。

就是说你传递的是引用,而不是值。

奖励园豆:5
爱编程的大叔 | 高人七级 |园豆:30839 | 2014-10-30 16:52

可以我有new了一个新的persons

List<Person> persons = new List<Person>();
foreach (var i in list)
 {
   persons.Add(i);
 }

将传递过来的List<Person> 加到新的Persons

xyz0 | 园豆:201 (菜鸟二级) | 2014-10-30 17:24

@xingthx: 这么说吧,

有三个人,张三、李四、王五

你放在列表L:河南人List里面

进入Form以后,你从河南人LIST将这三个移出来,放到中国人LIST里面。

你觉得有用吗?

如果你想这三个人不同,那你需要

List<Person> persons = new List<Person>();
 foreach (person obj in list)
 {
    persons.Add(obj.clone());
}
 foreach (var p in persons)
 {
       p.Age = p.Age + 1;
 }

现在问题来了,制作克隆人技术哪家强?

爱编程的大叔 | 园豆:30839 (高人七级) | 2014-10-30 17:29

@爱编程的大叔: 新的List添加的还是引用。我明白了,感谢!!

xyz0 | 园豆:201 (菜鸟二级) | 2014-10-30 20:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册