我编写了一个程序,把员工的姓名、编号、职位、薪水储存在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();
}
}
{
//
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();
}
}
}
}
return temp;
}
这样估计行吧
后面没有返回
else
{
MessageBox .Show ("查无此人");
return null;
}
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
下面那个也是
第一,编译问题: for 后面没有返回值,导致编译失败,可以为for最后一句之后添加返回值,这个问题上面已经说过了。第二,这种在for中写MessageBox的做法并不好,应该是返回一个值,在调用这个方法的代码中再决定是否使用MessageBox第三、程序中使用到的StreamReader 没有关闭,应该使用try...finally...,在finally中关闭文件
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
{
if (temp[j] == ID)
return temp;
else
if (temp[j] == name)
return temp;
else
{
MessageBox.Show("查无此人");
return null;
}
}
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;
查找过程中,如果有就返回temp
一旦查找过程中没有找到,他就循环到底,直接在for循环外边写messageBox和return null
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;