我编写了一个程序,把员工的姓名、编号、职位、薪水储存在TXT中,储存好了之后,我用streamreader读出来,又用split()把他们分成四个字符串存储到数组里面,并把数组作为返回值返回给Form3,之后就是输出了。大家帮我看一下,系统报错提示:  并非所有代码路径都返回值,而卧又找不到错误,大家帮我看一下,要怎么修改~~!!谢谢了类中的方法:        public string[] sehPeo(string name,string ID)
        {
            //
            string path = @"../members.txt";
            StreamReader sr = new StreamReader(path);
            //sr.BaseStream.Seek(0, SeekOrigin.Begin);
            string[] content=new string[0];
            int row=0;
            while(!sr.EndOfStream)
            {
                row++;
            }
            for(int i=0;i<row;i++)
            {
                content[i]=sr.ReadLine();
                string[] temp = content[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for(int j=0;j<content[i].Length;j++)
                {
                    if (temp[j] == ID)
                        return temp;
                    else
                        if (temp[j] == name)
                            return temp;
                        else MessageBox.Show("查无此人");
                }
            }
            
        }        public string[] sehPeo(string str)
        {
            //
            string path = @"../members.txt";
            StreamReader sr = new StreamReader(path);
            string[] content = new string[0];
            int row = 0;
            while (!sr.EndOfStream)
            {
                row++;
            }
            for (int i = 0; i < row; i++)
            {
                content[i] = sr.ReadLine();
                string[] temp = content[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < content[i].Length; j++)
                {
                    if (temp[j] == str)
                        return temp;
                    else MessageBox .Show ("查无此人");
                }
            }        }
Foem3:
public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }        private void Form3_Load(object sender, EventArgs e)
        {        }        private void button1_Click(object sender, EventArgs e)
        {
            members ms = new members();
            string name = textBox1.Text;
            string ID = textBox2.Text;
            string[] array=new string[0];
            if (name==null&&ID==null)
            {
                MessageBox.Show ("姓名和ID为空");
            }
            else
                if(name==null)
                    array=ms.sehPeo(ID);
                else 
                    if(ID==null)
                    array=ms.sehPeo (name);
                    else array=ms.sehPeo (name,ID);        Form7 f7 = new Form7(array[0],array[1],array[2],array[3] );
        f7.Show();
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }   
    }

解决方案 »

  1.   

    在最后加一句   return null;
      

  2.   

                           else MessageBox.Show("查无此人");
                    }
                }
                return temp;
            }
    这样估计行吧
      

  3.   

    else MessageBox .Show ("查无此人");
    后面没有返回
    else 
    {
    MessageBox .Show ("查无此人");
    return null;
    }
      

  4.   

    有 很多地方没有返回值
     for(int i=0;i<row;i++)
                {
                    content[i]=sr.ReadLine();
                    string[] temp = content[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    for(int j=0;j<content[i].Length;j++)
                    {
                        if (temp[j] == ID)
                            return temp;
                        else
                            if (temp[j] == name)
                                return temp;
                            else MessageBox.Show("查无此人");
                    }
                }
    如果row=0的时候就没有返回值
    要在最外层加 return
    下面那个也是
      

  5.   

    sehPeo方法有可能出现没有返回值得情况,所以最外层要有return
      

  6.   

    这段代码有三个问题:
    第一,编译问题: for 后面没有返回值,导致编译失败,可以为for最后一句之后添加返回值,这个问题上面已经说过了。第二,这种在for中写MessageBox的做法并不好,应该是返回一个值,在调用这个方法的代码中再决定是否使用MessageBox第三、程序中使用到的StreamReader 没有关闭,应该使用try...finally...,在finally中关闭文件
      

  7.   

    Visual Studio 200X 的SP1补丁包,不是Windows补丁包给你个地址吧!
    vs2008 sp1 下载地址  
     
    English Version:http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=27673c47-b3b5-4c67-bd99-84e525b5ce61 Chinese Version:http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=27673c47-b3b5-4c67-bd99-84e525b5ce61
     
      

  8.   

    中文的或者英文的看你实际情况了,注意这是VS2008的SP1 如果你用的是VS2005,那就需要去网上搜索一下了!
      

  9.   

    为什么我在加了返回值之后,他又警告说:检测到无法访问的代码,就是那个j++for(int j=0;j<content[i].Length;j++)
                    {
                        if (temp[j] == ID)
                            return temp;
                        else
                            if (temp[j] == name)
                                return temp;
                            else
                            {
                                MessageBox.Show("查无此人");
                                return null;
                            }
                    }
      

  10.   

    帮你给了下,没有错误了:
            string path = @"../members.txt";
            StreamReader sr = new StreamReader(path);
            //sr.BaseStream.Seek(0, SeekOrigin.Begin);
            string[] content = new string[0];
            int row = 0;
            while (!sr.EndOfStream)
            {
                row++;
            }
            for (int i = 0; i < row; i++)
            {
                content[i] = sr.ReadLine();
                string[] temp = content[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < content[i].Length; j++)
                {
                    if (temp[j] == ID || temp[j] == name)
                    {
                        return temp;
                    }
                    else
                        continue;
                }
            }
            MessageBox.Show("查无此人");
            return null;
      

  11.   

    楼主:
    查找过程中,如果有就返回temp
    一旦查找过程中没有找到,他就循环到底,直接在for循环外边写messageBox和return null
      

  12.   

    楼主源代码逻辑错误,红色部分是我在18楼的基础上修改的。string path = @"../members.txt";
    StreamReader sr = new StreamReader(path);
    //sr.BaseStream.Seek(0, SeekOrigin.Begin);
    string[] content = new string[0];
    int row = 0;
    while (!sr.EndOfStream)
    {
        row++;
    }
    for (int i = 0; i < row; i++)
    {
        content[i] = sr.ReadLine();
        string[] temp = content[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        for (int j = 0; j < temp.Length; j++)
        {
            if (temp[j] == ID || temp[j] == name)
            {
                return temp;
            }
            else
                continue;
        }
    }
    MessageBox.Show("查无此人");
    return null;