ASP.Net - 访问unstatic类的静态方法时出错

时间:2013-01-16 18:52:24

标签: c# asp.net

我正在使用Microsoft Visual Web Developer和C#based-ASP.Net开发一个网站作为学校项目。

我已经实现了一个简单的类来表示用户,因为它在数据库中形成。班级 具有表示db中的列的成员,以及一些用于从DataTable结果创建实例的静态方法。使用以下代码(MyDbase是我们给出的一些帮助类):

//User.cs, placed in A
using System;

public class User
{
    private int id;
    private String email;
    private String firstName;
    private String lastName;
    private String username;
    private String passwd;
    private String birthDate;
    private bool isMale;
    private String phone;
    private String avatarUrl;
    private bool isAdmin;

    ... //Accessors and mutators

    public User( int _id, String _email, String _firstName, String _lastName, String _username, String _passwd,
                String _birthDate, bool _isMale, String _phone, String _avatarUrl, bool _isAdmin )
    {
        this.id = _id;
        this.email = _email;
        this.firstName = _firstName;
        this.lastName = _lastName;
        this.username = _username;
        this.passwd = _passwd;
        this.birthDate = _birthDate;
        this.isMale = _isMale;
        this.phone = _phone;
        this.avatarUrl = _avatarUrl;
        this.isAdmin = _isAdmin;
    }

    public static User getUserForSqlResult(System.Data.DataTable result)
    {
        User newUser = null;
        if (result.Rows.Count == 1)
        {
            newUser = new User((int)result.Rows[0]["id"], (String)result.Rows[0]["email"], 
                                (String)result.Rows[0]["firstName"], (String)result.Rows[0]["lastName"], (String)result.Rows[0]["username"],
                                (String)result.Rows[0]["passwd"], (String)result.Rows[0]["birthDate"], (bool)result.Rows[0]["isMale"],
                                (String)result.Rows[0]["phone"], (String)result.Rows[0]["avatarUrl"], (bool)result.Rows[0]["isAdmin"]);
        }

        return newUser;
    }

    public static User getUserById(int id)
    {
        System.Data.DataTable dt = MyDbase.SelectFromTable("select * from tUsers where id=" + id, "db.mdb");
        return getUserForSqlResult(dt);
    }

    public void saveUserChanges()
    {
        MyDbase.ChangeTable("UPDATE tUsers SET email='" + this.email + "', firstName='" + this.firstName + "', lastName='" + this.lastName +
                            "', passwd='" + this.passwd + "', birthDate='" + this.birthDate + "', phone='" + this.phone + "', avatarUrl='" +
                            this.avatarUrl + "' WHERE id=" + this.id , "db.mdb");
    }
}

我尝试运行以下示例代码,以确保select& update函数正常工作(Default.aspx):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        User user = User.getUserById(3);
        user.setEmail("different@email.com");
        user.saveUserChanges();
    }
}

但是,我收到以下错误:

  

错误1'System.Security.Principal.IPrincipal'不包含   'getUserById'的定义,没有扩展方法'getUserById'   接受第一个类型的参数   可以找到'System.Security.Principal.IPrincipal'(你错过了吗?   using指令或程序集引用?)

为什么会出现这种情况的任何想法? TNX!

3 个答案:

答案 0 :(得分:4)

问题是Page有一个名为User的属性。关于评估User.getUserById(),它推断的不是您的User类。请改用完整类型命名空间:

protected void Page_Load(object sender, EventArgs e)
{

    My.Namespace.User user = My.Namespace.User.getUserById(3);
    user.setEmail("different@email.com");
    user.saveUserChanges();
}

如果您的类没有命名空间,请添加一个。但是,大多数项目都支持默认命名空间。您可以通过查看项目属性来了解这是什么。

答案 1 :(得分:0)

您正在将User类与页面的User属性混合。

答案 2 :(得分:0)

在User类中添加一个名称空间,以消除Page.User属性和User类

之间的歧义
using System;

namespace MyUser
{
    public class User
    {
       ....
    }
}

然后在Page_Load

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

        MyUser.User user = MyUser.User.getUserById(3);
        user.setEmail("different@email.com");
        user.saveUserChanges();
    }
}