简体   繁体   English

ASP.Net-错误访问非静态类的静态方法

[英]ASP.Net - Error accessing static method of unstatic class

I'm developing a website as a school project using Microsoft Visual Web Developer and C# based-ASP.Net. 我正在使用Microsoft Visual Web Developer和基于C#的ASP.Net将网站开发为学校项目。

I have implemented a simple class to represent a User as it is formed in a db. 我实现了一个简单的类来表示用户,因为它是在数据库中形成的。 The class Has members that represent the columns in the db, and a couple of static methods to create an instance from a DataTable result. 该类具有表示db中列的成员,以及一些静态方法,这些方法可以从DataTable结果中创建实例。 The following code is used (MyDbase is some help class we were given): 使用以下代码(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");
    }
}

I tried to run the following sample code just to make sure the select&update functions are working (Default.aspx): 我试图运行以下示例代码只是为了确保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();
    }
}

However, I get the following error: 但是,出现以下错误:

Error 1 'System.Security.Principal.IPrincipal' does not contain a definition for 'getUserById' and no extension method 'getUserById' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?) 错误1'System.Security.Principal.IPrincipal'不包含'getUserById'的定义,并且找不到扩展方法'getUserById'接受类型为'System.Security.Principal.IPrincipal'的第一个参数(您是否缺少使用指令还是程序集引用?)

Any ideas why this might happen? 任何想法为什么会发生这种情况? Tnx! TNX!

The problem is Page has a property called User . 问题是Page具有名为User的属性。 When it comes to evalute User.getUserById() it is inferring that instead of your User class. 当涉及评估User.getUserById()它是在推断您的User类。 Use the full type namespace instead: 请使用完整类型的名称空间:

protected void Page_Load(object sender, EventArgs e)
{

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

If your class doesn't have a namespace, add one. 如果您的班级没有命名空间,请添加一个。 However, most projects support a default namespace. 但是,大多数项目都支持默认名称空间。 You can find out what this is by looking at the project properties. 您可以通过查看项目属性来了解这是什么。

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

Add a namespace to your User class to disambiguate between Page.User property and your User class 向您的User类添加名称空间以区分Page.User属性和User类

using System;

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

then in the Page_Load 然后在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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM