我这样写一个类:public class UserAdmin
{
    DBClass dbo = new DBClass();
public UserAdmin()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
    public int memeberId { get; set; }
    public string userName { get; set; }
    public string userPass { get; set; }
    public int sex { get; set; }
    public string trueName { get; set; }
    public string question { get; set; }
    public string answer { get; set; }
    public string email { get; set; }
    public string city { get; set; }
    public string address { get; set; }
    public string postCode { get; set; }    //*************************** 首页登录验证并返回用户ID*******************************
    public int UserLogin(UserAdmin user)
    {
        int id;
        SqlParameter[] param ={dbo.MakeParam_Input("@UserName",SqlDbType.VarChar,50,user.userName),
                              dbo.MakeParam_Input("@UserPass",SqlDbType.VarChar,50,user.userPass),
                              dbo.MakeParam("@MemberID")};
        id = dbo.RunProc("proc_UserLogin", param, "@MemberID");
        return id;    }
}
然后在登录网页.cs这样写:UserAdmin user = new UserAdmin {userName=txtName.Text,userPass=txtPwd.Text};
int id = user.UserLogin(user);
if(id!=-1&&id!=-10000)
{
//......
}我同学说类中的变量全都公开,安全性不好,应该是:
private string userName;
private string userPass;
public string UserName
{
get{ return userName;}
set{userName=Value;}
}public string UserPass
{
get{ return userPass;}
set{ userPass=Value;}
}
使用的时候就:UserAdmin user=new UserAdmin();
user.UserName=txtName.text;
user.UserPass=txtPwd.text;
//......我觉我的写法没什么不好的呀。
声明为private,虽在外部不能直接访问变量,但不也是可以从属性访问吗?安全性也不怎么样啊,
而且又麻烦,要写10多个属性,成员多的时候更加麻烦。
大家来说说看吧,大家是怎么写的?

解决方案 »

  1.   

    你要理解C#这种语言,本来就不提倡用public.
      

  2.   

    两种完全没有区别...第一种也是属性,不过是语法糖而已...不过公共成员命名规则最好遵循Pascal命名法,Camel命名仅用于私有成员,所以被你那个只懂皮毛的同学误会为字段...属性比字段更安全更灵活是不用质疑的...
      

  3.   

    Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its functionality but make it “sweeter” for humans to use. Syntactic sugar gives the programmer (designer, in the case of specification computer languages) an alternative way of coding (specifying) that is often more practical, more conducive to a better programming style, or more natural to read. However, it does not typically affect the expressiveness of the formalism or permit the language to do something new.