an object reference is required for the non-static field
James
Guys, does anyone know the answer?
get an object reference is required for the non-static field from EN Bilgi.
[Solved] error An object reference is required for the non
CodeProject SenseAI
All Message Boards...
Application Lifecycle>
Running a Business Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET JavaScript Internet of Things C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools
Objective-C and Swift
Database Hardware & Devices> System Admin Hosting and Servers Java Linux Programming
.NET (Core and Framework)
Android iOS Mobile WPF Visual Basic Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
Projemde OLuşan Hata [An object reference is required for the non
Bir soru sorun Hızlı Erişim
Arama ile ilgili konular
Soran
Projemde OLuşan Hata [An object reference is required for the non-static field, method, or property ]
Visual Studio Development
> Visual C#
Soru
0
Oylamak İçin Oturum Aç
Merhaba ,
Projemi Geliştirken Bit Extensions ve Uptades bölümünden Bir Araç Ekledim Projemi Yeniden Actıgımda Az Önce Derlenmedi Hiç Hat Vermeyen Program Hata Fırkatmaya BAsladı Ne Yapdıysam Çözemedim.
Bir Baksanız Yeniden Tüm KOntroleri YAzmak İştemiyorum NAsıl bi Çözüm Üretebilirim
Ekledigim Plugin İse : .NET DEMON
Bag Connection Sorun Yokdu..
An object reference is required for the non-static field, method, or property
23 Aralık 2012 Pazar 20:24
Yanıt | Alıntı
Tüm Yanıtlar
0
Oylamak İçin Oturum Aç
hazırlamış olduğunuz metodu static olarak tanımlayınız. Static bir metod içinde static olmayan bir üye kullanımında bulunuyorsunuz..
Düzenleyen burhan süleyman vuralEditor 24 Aralık 2012 Pazartesi 18:23
24 Aralık 2012 Pazartesi 18:20
Yanıt | Alıntı Yanıtlayıcı 0
Oylamak İçin Oturum Aç
Merhaba
Olebdconnetions Bag = new Olebdconnetions ("Database Yolu")
Baglantı Stringinimi Static Yapmalıyım..
25 Aralık 2012 Salı 06:17
Yanıt | Alıntı 0
Oylamak İçin Oturum Aç
şimdi sırf bu satırdan dolayı bu hatayı vermez eğer bu satırı global alanda tanımladı iseniz ve yine o global alanda bu Bag nesnesini kullanmaya çalışıyorsanız hata verir. Bu tanımlama yerleri önemli...
Eğer dediğim gibi ise o global alanda public static OledbConnection Bag=............. şeklinde nesneyi oluşturursanız yine o global alanda bu nesneyi çağırıp kullanabilirsiniz.
Zaten resimde de bu tanımlamaları o global alanda yaptığınız görülüyor...
25 Aralık 2012 Salı 06:45
Yanıt | Alıntı Yanıtlayıcı 0
Oylamak İçin Oturum Aç
Merhaba ,
Bir Anda 23 Hata Almamın Sebebi Nedir Hiçbir Kod müdahelesi yapmadım...
25 Aralık 2012 Salı 10:35
Yanıt | Alıntı 0
Oylamak İçin Oturum Aç
Kodları tam olarak görüntüleyebilirmisiniz
25 Aralık 2012 Salı 10:38
Yanıt | Alıntı Yanıtlayıcı 0
Oylamak İçin Oturum Aç
Form Tanımlamada Alıyorum Sadece Hatayı Kodların hepsi
using System;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.OleDb;
using System.Data;
using System.Drawing;
using DevComponents.DotNetBar.Validator;
namespace Desi_Hesaplama_1._0
{
public partial class FrmMusterı_Tanımlama : Office2007Form
{
public FrmMusterı_Tanımlama()
{
InitializeComponent();
OleDbConnection Baglantı = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database.accdb");
}
DataSet dtst = new DataSet();
public OleDbDataAdapter adtr = new OleDbDataAdapter();
OleDbCommand Komut = new OleDbCommand();
///
/// Baglantı Kontrol Metodu
/// void BagKOntrol() {
if (Baglantı.State == ConnectionState.Open)
{ Baglantı.Close(); } } void Güncelle() {
OleDbDataAdapter adtr = new OleDbDataAdapter("select * From Verıal", Baglantı);
adtr.Fill(dtst, "Verıal");
dataGridView1.DataSource = dtst.Tables["Verıal"];
adtr.Dispose();
dataGridView1.Columns[0].Visible = false;
Baglantı.Close(); }
private void Musterı_Tanımlama_Load(object sender, EventArgs e)
{ timer1.Start(); Baglantı.Open(); Güncelle(); }
private void timer1_Tick(object sender, EventArgs e)
{
TxtIslem.Text = String.Format("{0} Saat : {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
int SutunSayısı = dataGridView1.Rows.Count;
label1.Text = "Kayıt Sayısı : " + SutunSayısı.ToString();
int SutunSayısı1 = dataGridView1.Rows.Count;
toolStripStatusLabel1.Text = "Bekleyen Kayıt Sayısı : " + SutunSayısı1.ToString();
int Sayı = SutunSayısı1;
toolStripProgressBar1.ForeColor = Color.Red;
toolStripProgressBar1.Value = Sayı;
}
static private void BtnKayıtEkle_Click(object sender, EventArgs e)
{ Baglantı.Close();
if (TxtIrsalıye.Text == "")
{
MessageBox.Show("İrsaliYe Bilgisi Boş Geçilemez ", "Bİlgi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return; }
if (TxtAdres.Text == "")
{
MessageBox.Show("Adres Bilgisi Boş Geçilemez ", "Bİlgi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return; }
if (Txttelefon.Text == "")
{
MessageBox.Show("Telefon Bilgisi Boş Geçilemez ", "Bİlgi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return; }
if (TxtEsktr.Text == "")
{
MessageBox.Show("Ekstra Bilgisi Boş Geçilemez ", "Bİlgi", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return; } try { Baglantı.Open();
Komut.Connection = Baglantı;
Komut.CommandText = String.Format("INSERT INTO Verıal(İrsaliye_No,Adı_Soyadı,Güncel_Adres,Telefon_No,Esktra_Bilgiler,İşlem_Zamanı) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')", TxtIrsalıye.Text.ToUpper(), TxtAdı.Text.ToUpper(), TxtAdres.Text.ToUpper(), Txttelefon.Text.ToUpper(), TxtEsktr.Text.ToUpper(), TxtIslem.Text);
Source : social.msdn.microsoft.com
c#
Consider: namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Cl...
CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
Ask Question
Asked 13 years, 1 month ago
Modified 4 months ago
Viewed 1.4m times 338 Consider:
namespace WindowsApplication1
{
public partial class Form1 : Form
{ public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//int[] val = { 0, 0};
int val;
if (textBox1.Text == "")
{
MessageBox.Show("Input any no");
} else {
val = Convert.ToInt32(textBox1.Text);
Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
ot1.Start(val); } }
private static void ReadData(object state)
{
System.Windows.Forms.Application.Run();
}
void setTextboxText(int result)
{
if (this.InvokeRequired)
{
this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
} else {
SetTextboxTextSafe(result);
} }
void SetTextboxTextSafe(int result)
{
label1.Text = result.ToString();
}
private static void SumData(object state)
{ int result;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{ result += i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
delegate void IntDelegate(int result);
private void button2_Click(object sender, EventArgs e)
{ Application.Exit(); } } }
Why is this error occurring?
An object reference is required for the nonstatic field, method, or property 'WindowsApplication1.Form1.setTextboxText(int)
c# Share
edited Jun 12, 2019 at 5:00
VertigoRay 5,3356 6 gold badges 37 37 silver badges 46 46 bronze badges
asked Jan 31, 2009 at 6:22
huda 3,7072 2 gold badges 19 19 silver badges 21 21 bronze badges Add a comment
9 Answers
Active Oldest Votes 471
It looks like you are calling a non static member (a property or method, specifically setTextboxText) from a static method (specifically SumData). You will need to either:
Make the called member static also:
static void setTextboxText(int result)
{
// Write static logic for setTextboxText.
// This may require a static singleton instance of Form1.
}
Create an instance of Form1 within the calling method:
private static void SumData(object state)
{ int result = 0;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{ result += i;
System.Threading.Thread.Sleep(1000);
}
Form1 frm1 = new Form1();
frm1.setTextboxText(result);
}
Passing in an instance of Form1 would be an option also.
Make the calling method a non-static instance method (of Form1):
private void SumData(object state)
{ int result = 0;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{ result += i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
More info about this error can be found on MSDN.
Share
edited Mar 31, 2020 at 23:01
dbc 89.3k18 18 gold badges 177 177 silver badges 279 279 bronze badges
answered Jan 31, 2009 at 6:28
user60456 Add a comment 23
For this case, where you want to get a Control of a Form and are receiving this error, then I have a little bypass for you.
Go to your Program.cs and change
Application.Run(new Form1());
to
public static Form1 form1 = new Form1(); // Place this var out of the constructor
Application.Run(form1);
Now you can access a control with
Program.form1.Also: Don't forget to set your Control-Access-Level to Public.
And yes I know, this answer does not fit to the question caller, but it fits to googlers who have this specific issue with controls.
Share
edited Oct 15, 2017 at 17:48
answered Jan 7, 2017 at 20:28
Simon Nitzsche 6146 6 silver badges 13 13 bronze badges Add a comment 20
You start a thread which runs the static method SumData. However, SumData calls SetTextboxText which isn't static. Thus you need an instance of your form to call SetTextboxText.
Share
answered Jan 31, 2009 at 8:40
Brian Rasmussen 112k34 34 gold badges 217 217 silver badges 309 309 bronze badges 18
This answer appears to restate the problem. It doesn't explain this produces an error. –
Robert Jan 6, 2016 at 6:16 1
@Robert Neither do all the other answers... this comment was pointless –
Shidouuu
Apr 28, 2021 at 0:46
Add a comment 8
Your method must be static
static void setTextboxText(int result)
{
if (this.InvokeRequired)
{
this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
} else {
SetTextboxTextSafe(result);
} }
Guys, does anyone know the answer?