标题:求问,为什么用foreach后 数组的每一个元素都被覆盖了
取消只看楼主
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
结帖率:66.67%
已结贴  问题点数:20 回复次数:4 
求问,为什么用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
qx6574173
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-12
得分:0 
回复 4楼 xydddaxia
修改这个错误以后还是一样的结果,新添加的数据还是会覆盖数组前面的内容
2012-03-19 16:02
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.116801 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved