using System;
using System.Collections.Generic;
using System.Text;
namespace 服装销售管理系统
{
    class UserManagerClass
    {
        private static string[] user = null;//"huaxiangchun9615102";
        private static int user_number ;
        
        static UserManagerClass() //静态构造函数来初始化静态变量
        {
            user[0] = "huaxiangchun9615102";
            user_number = 0;
        }
         
        //member function
        
        public static bool addUser(string elem)
        {
            for (int i = 0; i != user.Length; ++i)
            {
                if (user[i] == elem)
                    return false;            }
            user_number++;
            user[user_number - 1] = elem;
            return true;
            
        }
        public static bool removeUser(string elem)
        {
            for (int i = 0; i != user.Length; ++i)
            {
                if (user[i] == elem)
                {
                    for (int j = i; j != user.Length; ++j)
                        user[j] = user[j + 1];
                    --user_number;
                    return true;
                }
                       
            }
            return false;
        }
        //compare member function
       public static bool InvildUser(string elem)
        {
            for (int i = 0; i != user.Length; ++i)
                if (user[i] != elem)
                    return false;
            return true;
        }
    }
}
我先写了上面这个类.本意是把用户名和密码存在一个字符串中,然后用静态构函预先存一个用户,作为默认用户
再在用户管理窗口调用类的静态方法添加 用户
 private void button3_Click(object sender, EventArgs e)
        {
            string userInfo = textBox1.Text + textBox2.Text;
            
            UserManagerClass.addUser(userInfo);
        }
抛出异常不知道是哪里错了?
高人请来指导指导下

解决方案 »

  1.   

    1.首先private static string[] user = null;这句话只是申明了一个数组变量,之后你并没有创建数组就直接user[0] = "huaxiangchun9615102"; 因此报错。
    所以要改为:private static string[] user = new string[10];
    2.数组长度一旦创建就固定了,所以这里要创建用户列表不适合用数组。建议private static List<string> user = new List<string>();