标题:求问,为什么用foreach后 数组的每一个元素都被覆盖了
只看楼主
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
结帖率:66.67%
已结贴  问题点数:20 回复次数:6 
求问,为什么用foreach后 数组的每一个元素都被覆盖了
求问,为什么用foreach后 数组的每一个元素都被覆盖了
第一个是自己定义的一个类,第二个是调用类写入一个ReadFile【】数组中。但遍历后加入的元素会把数组前面的元素覆盖了,求解怎么解决这个问题?

    {
        public string ReadName;
        public string FilePath;
        public ReadFile()
        {
            ReadName="Name";
            FilePath="Path";
        }
        public ReadFile(string _name,string _path)
        {
            for (int i = 0; _name != null; i++)
            {
                ReadName = _name;
                FilePath = _path;
            }
 
        }
    }
   

             private void Form1_Load(object sender, EventArgs e)
        {


            string[] name = new string [512];
            
            ReadFile[] FNames=new ReadFile[512];
            ReadFile RF = new ReadFile();
            int length =0;
            TSTime.Text = System.DateTime.Now.ToString();
            string path=() +@"\Mydiary\";
            if(Directory.Exists(path)==false)
            {
                Directory.CreateDirectory(path);
            }
            foreach (string FileName in Directory.GetFiles(path))
            {

                string FName;
              FName = FileName.Substring(path.Length,FileName.Length-path.Length-4);
              RF.ReadName = FName;
              RF.FilePath = FileName;
              FNames.SetValue(RF,length);
              length++;
            }
            treeView.Nodes.Clear();
            AddNodes(FNames);
        }
搜索更多相关主题的帖子: null public 元素 
2012-03-16 23:17
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
得分:0 
第一段代码的类是
程序代码:
   public class ReadFile
    {
        public string ReadName;
        public string FilePath;
        public ReadFile()
        {
            ReadName="Name";
            FilePath="Path";
        }
        public ReadFile(string _name,string _path)
        {
            for (int i = 0; _name != null; i++)
            {
                ReadName = _name;
                FilePath = _path;
            }

        }
    }
   
2012-03-16 23:19
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
得分:0 
求大神帮助啊!帖子不能沉,我自己顶!!
2012-03-18 18:44
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
得分:10 
public ReadFile(string _name,string _path)
        {
            for (int i = 0; _name != null; i++)
            {
                ReadName = _name;
                FilePath = _path;
            }

        }
for里面是个死循环吧

站在春哥的肩膀上
2012-03-19 11:23
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
得分:0 
回复 4楼 xydddaxia
修改这个错误以后还是一样的结果,新添加的数据还是会覆盖数组前面的内容
2012-03-19 16:02
abcd25322
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2010-10-25
得分:10 
估计是你的AddNodes方法算法有错。。。你把AddNodes方法贴出来啊!
2012-03-19 20:42
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
得分:0 
回复 6楼 abcd25322
我找到错误的地方了,应该在FOREACH中新建新的ReadFile类
程序代码:
        private void Form1_Load(object sender, EventArgs e)
        {

            string[] name = new string [512];
            //if (this.AdminID == null || this.PassWord == null)
            //{
            //    MessageBox.Show("请输入用户名与初始密码!", "第一次登陆");
            //    Form3 F3 = new Form3();
            //    F3.Show(this);
            //}
            //else
            //{
            //    Form2 FrmLogin = new Form2();
            //    FrmLogin.ShowDialog(this);
            //}
            ReadFile[] FNames=new ReadFile[512];
            int length =0;
            TSTime.Text = System.DateTime.Now.ToString();
            string patten = @"\.smd$";
            Regex r = new Regex(patten);
          
            if(Directory.Exists(path)==false)
            {
                Directory.CreateDirectory(path);
            }
            foreach (string FileName in Directory.GetFiles(path))
            {
                string FName;
               ReadFile RF = new ReadFile();
               Match m = r.Match(FileName);
               FName = FileName.Substring(path.Length, FileName.Length - path.Length - 4);
                if(m.Success)
                {
              RF.ReadName = FName;
              RF.FilePath = FileName;
              FNames[length++] = RF;
                }
            }
            treeView.Nodes.Clear();
            AddNodes(FNames);
        }

 
2012-03-19 21:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-363413-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.189984 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved