C# 使用了未赋值的变量w

来源:百度知道 编辑:UC知道 时间:2024/07/02 04:14:18
namespace ConsoleStudent
{
public class Student
{
public Student()
{
}
}
class Program
{
public static void Create(Student s)
{
}
public static void Delete(Student n)
{
}
public static void Update(student m)
{
}
public static void Retrieve(student k)
{
}
static void Main(string[] args)
{
Student w;
char a;
bool flag=true;
while(flag)
{
Console.WriteLine("增加请输入c,删除请输入d,修改请输入u,查找请输入r,退出请按其他键");
a=char.Parse(Console.ReadLine());
switch (a)
{
case 'c':
Create(w); break;
case 'd':
Delete(w); break;
case 'u':
Update(w); break;
case 'r':
Retrieve(w); break;
default:
Console.WriteLine("感谢您的使用!"); flag = false; break;
}
}
}
}
}
编译后说是使用了未赋值的变量w,大家帮忙看看是怎么改正呀
还是不对啊。。。再问一个问题,怎么将数据添加到数组里呢

Student w;这样只是定义一个变量而已,并没有初始化他,所以会报错
就像你定义string s; 如果直接使用s的话也会报同样的错误
只要Student w = new Student();实例他就可以了

数组是无法添加数据进去的,因为定义数组时已经指定大小了
除非用集合来
List<int> numbers = new List<int>();
numbers.Add(1);这样就可以随便添加删除了,<>里面是放数据类型的
还有其他集合,自己看资料吧

w没有赋值,你的w是一个类的对象,使用的时候没有赋值。

Student w = new Student();

Student w;
引用一个就好了,如楼上