非静态错误,找不到原因

发布于 2025-01-03 12:30:12 字数 1354 浏览 1 评论 0原文

我收到“非静态字段、方法或属性 Skirmer_Final.Nyhed.FK_Nyhed_ID.get 需要对象引用”错误。我不知道出了什么问题。

我的代码

public class Nyhed
{
    public int FK_Status_ID { get; set; }
    public int FK_Nyhed_ID { get; set; }

    public static List<Nyhed> GetByStatus(int ID, SqlConnection connection)
    {
        List<Nyhed> result = new List<Nyhed>();

        using (var command = new SqlCommand("Select FK_Nyhed_ID from Status_Kan_Se_Nyhed where FK_Status_ID=@id"))
        {
            command.Connection = connection;

            command.Parameters.AddWithValue("id", ID);

            SqlDataReader reader = command.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    Nyhed StatusKanSeNyhed = new Nyhed();
                    StatusKanSeNyhed.FK_Status_ID = ID;
                    StatusKanSeNyhed.FK_Nyhed_ID = reader.GetInt32(0);
                    result.Add(StatusKanSeNyhed);
                }
            }
            finally
            {
                reader.Close();
            }
            foreach (Nyhed N in result)
            {
                N.status = Status.GetByID(FK_Status_ID, connection);
                N.nyhed = Nyhed.GetByID(FK_Nyhed_ID, connection);
            }
        }
        return result;
    }
}

你能看到错误吗?

I'm getting an "An object reference is required for the non-static field, method, or property Skirmer_Final.Nyhed.FK_Nyhed_ID.get" error. And I can't figure out whats wrong.

My code

public class Nyhed
{
    public int FK_Status_ID { get; set; }
    public int FK_Nyhed_ID { get; set; }

    public static List<Nyhed> GetByStatus(int ID, SqlConnection connection)
    {
        List<Nyhed> result = new List<Nyhed>();

        using (var command = new SqlCommand("Select FK_Nyhed_ID from Status_Kan_Se_Nyhed where FK_Status_ID=@id"))
        {
            command.Connection = connection;

            command.Parameters.AddWithValue("id", ID);

            SqlDataReader reader = command.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    Nyhed StatusKanSeNyhed = new Nyhed();
                    StatusKanSeNyhed.FK_Status_ID = ID;
                    StatusKanSeNyhed.FK_Nyhed_ID = reader.GetInt32(0);
                    result.Add(StatusKanSeNyhed);
                }
            }
            finally
            {
                reader.Close();
            }
            foreach (Nyhed N in result)
            {
                N.status = Status.GetByID(FK_Status_ID, connection);
                N.nyhed = Nyhed.GetByID(FK_Nyhed_ID, connection);
            }
        }
        return result;
    }
}

Can you see the error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

却一份温柔 2025-01-10 12:30:12

FK_Nyhed_ID 是一个属性。因此,您需要通过一个对象来引用它。我猜问题出在这里:

foreach (Nyhed N in result) {
    N.status = Status.GetByID(FK_Status_ID, connection);
    N.nyhed = Nyhed.GetByID(FK_Nyhed_ID, connection);
}

您之前在 StatusKanSeNyhed 实例上引用了 FK_Nyhed_ID,所以我猜您会想要引用 N.FK_Nyhed_ID< /代码> 下面

foreach (Nyhed N in result) {
    N.status = Status.GetByID(FK_Status_ID, connection);
    N.nyhed = Nyhed.GetByID(N.FK_Nyhed_ID, connection);  //<----- added object reference
}

FK_Nyhed_ID is a property. As such, you need to reference it through an object. I'm guessing the problem is here:

foreach (Nyhed N in result) {
    N.status = Status.GetByID(FK_Status_ID, connection);
    N.nyhed = Nyhed.GetByID(FK_Nyhed_ID, connection);
}

You previously referenced FK_Nyhed_ID on the StatusKanSeNyhed instance, so I'm guessing you'll want to reference N.FK_Nyhed_ID below

foreach (Nyhed N in result) {
    N.status = Status.GetByID(FK_Status_ID, connection);
    N.nyhed = Nyhed.GetByID(N.FK_Nyhed_ID, connection);  //<----- added object reference
}
痴梦一场 2025-01-10 12:30:12

我猜您想编写

        foreach (Nyhed N in result)
        {
            N.status = Status.GetByID(N.FK_Status_ID, connection);
            N.nyhed = Nyhed.GetByID(N.FK_Nyhed_ID, connection);
        }

并添加缺少的 N.

I guess the you wanted to write

        foreach (Nyhed N in result)
        {
            N.status = Status.GetByID(N.FK_Status_ID, connection);
            N.nyhed = Nyhed.GetByID(N.FK_Nyhed_ID, connection);
        }

and add the missing N.

要走干脆点 2025-01-10 12:30:12

FK_Status_ID 是一个实例属性,因此无法从静态方法访问它。
您可以将其设为静态或将方法更改为实例方法。

FK_Status_ID is an instance property so it cannot be accessed from a static method.
You can either make it static or change your method to an instance method.

一枫情书 2025-01-10 12:30:12

问题在于:

foreach (Nyhed N in result)
        {
            N.status = Status.GetByID(FK_Status_ID, connection);
            N.nyhed = Nyhed.GetByID(FK_Nyhed_ID, connection);
        }

您正在尝试读取公共成员 FK_Status_ID,该成员只能通过对象的实例访问。

The problem is here:

foreach (Nyhed N in result)
        {
            N.status = Status.GetByID(FK_Status_ID, connection);
            N.nyhed = Nyhed.GetByID(FK_Nyhed_ID, connection);
        }

You are trying to read public member FK_Status_ID, whitch is accessible only though an instance of an object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文