非静态字段方法或属性需要对象引用 [英] An object reference is required for the non-static field method or property

查看:104
本文介绍了非静态字段方法或属性需要对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试  以访问labelNummer.Text从我的表单中的用户控件。但是现在我得到了错误,非静态
字段方法或属性需要
n对象引用。我需要帮助。 

I'm trying to access labelNummer.Text from my User Control in my form. But now i get the error an object reference is required for the non-static field method or property. I need help with this. 

Upartijen.labelNummer.Text在我的表单中给出错误。

Upartijen.labelNummer.Text gives the error in my form.

if (idPartijen.Contains(uPartijen.labelNummer.Text))
            {
                con.SqlQuery("SELECT * FROM `kandidaat` where `partijnummer` =@nummer ");
                con.Cmd.Parameters.Add("@nummer", MySql.Data.MySqlClient.MySqlDbType.VarString).Value = uPartijen.labelNummer.Text;

                foreach (DataRow dr in con.QueryEx().Rows)
                {
                    uKandidaten proitem = new uKandidaten(this);

                    Voornaam = dr[2].ToString();
                    Achternaam = dr[3].ToString();
                    NummerKandidaat = dr[6].ToString();

                    proitem.KandidaatNaam = Voornaam;
                    proitem.KandidaatAchterNaam = Achternaam;
                    proitem.KandidaatNummer = NummerKandidaat;

                    kandidatenlist.Add(proitem);
                    idPartijen.Add(NummerKandidaat);

                }
                panelPartijen.Controls.Clear();
                panelPartijen.Controls.AddRange(kandidatenlist.ToArray());

            }


这是我的用户控制

This is my User Control

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;


namespace Stemcomputer
{
    public partial class uPartijen : UserControl
    {
        public uPartijen(StemSysteem  form1)
        {
            InitializeComponent();
             _Form1 = form1;
        }

      

        public StemSysteem _Form1;

        public string Nummer;

        public string KandidaatNummer
        {
            get
            {
                return labelNummer.Text;
            }

            set
            {
                labelNummer.Text = value;
            }
        }

推荐答案

uPartijen labelNummer 文字

uPartijen.labelNummer.Text

无论该控件处于何种状态,您都可以创建一个公共字符串Method()来返回
的范围 uPartijen < span style ="">。 labelNummer 文字。

Whereever that control is at, you could make a public string Method() to return the valute of the uPartijen.labelNummer.Text.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler -messages / cs0120

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0120


这篇关于非静态字段方法或属性需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆