CS0120: An object reference is required for the non-static field, method, or property 'Student_Form.StudentID_textBox'
Dear All,
I encounter an error "CS0120: An object reference is required for the non-static field, method, or property 'Student_Form.StudentID_textBox'".
I try to assign a value "0" to the textBox as shown in the screen capture below in the design time. I still get this error. Could anybody tell me how to fix it?
C#
-
Jack J Jun 24,296 Reputation points • Microsoft Vendor
2022-09-06T05:33:09.737+00:00 @BenTam , Welcome to Microsoft Q&A, what is the value for the variable
s_StudentID
? How do you defined the textbox in the form? -
BenTam 1,581 Reputation points
2022-09-07T04:17:31.753+00:00 Hi @Jack J Jun
The value to be assigned to the "StudentID_textBox.Text" is "000038". At the moment the error occurs the value of the "StudentID_textBox.Text" is "null", not "0".
The error shows that "000038" can't overwrite the "null" value. Any idea about this error?
-
RLWA32 40,856 Reputation points
2022-09-07T10:45:57.823+00:00 At the point where the exception is thrown the StudentID_textBox control has a null value. So at some point after Form creation when the control is instantiated it is being set to null.
For example, if you created a Form with a textbox control and two buttons as follows you can reproduce the error:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { StudentID_textBox = null; } private void button2_Click(object sender, EventArgs e) { try { StudentID_textBox.Text = "New Value"; } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } } }
-
Karen Payne MVP 35,196 Reputation points
2022-09-07T11:09:30+00:00 Where exactly is the exception being thrown, in a form or in a class? If by chance you are passing the TextBox to a class, don't, instead use a delegate/event.
-
-
RLWA32 40,856 Reputation points
2022-09-07T13:37:50.91+00:00 You need to determine why your code is using a null reference in the StudentID_textBox control.
-
AgaveJoe 26,151 Reputation points
2022-09-07T14:27:33.16+00:00 However, if I want to set the TextBox.Text to s_StudentID (now 000038, then other values), what should I do?
m_StudentID is defined an enumerable table. Then you set s_StudentID to m_StudentID.ToString("00000"). Basically, the code we can see makes no logical sense.
Also, please stop posting screenshot of your code!!!! Post your code on GitHub or on the forum using the "code Sample" button. Having the code helps us to help you.
-
Ronald Rex 1,666 Reputation points
2022-09-07T16:41:13.827+00:00 How would you use a delegate to do this? Thank You !!!
-
-
Jack J Jun 24,296 Reputation points • Microsoft Vendor
2022-09-14T05:20:11.053+00:00 @BenTam , based on my your picture, It seems that I reproduced your problem. If you only statement your textbox without creating instance of the textbox, it will throw the error. I suggest that you could create a new textbox by dragging the textbox from the toolbox to give that value.
-
BenTam 1,581 Reputation points
2022-09-14T15:14:31.897+00:00 Hi @Jack J Jun
Thanks for your reply. I have already given the textbox a value of '0'. But it then changes to null. Do you mean that I assign "000038" to it? The value of the textbox will change according to the data retrieved from the SQLServer.
-
AgaveJoe 26,151 Reputation points
2022-09-14T15:28:18.837+00:00 My logic is that "m_StudentID" is the int value retrieved from SQLserver.
This is not clear from your screenshot. It is counter productive to force the community to guess. Why do you refuse to share your source code?
Anyway, the Compiler Error CS0120 error is a compiler error telling you that the complier cannot find the StudentID_textBox class. This type of error is usually related to a missing using statement but since this is a WinForm it is not clear how you managed to do this. Maybe the StudentID_textBox is in another project??????
Also, it's not clear how you are able to run the project with the complier error.
For the hundredth time! Provide the source code if you want a community code review. Otherwise, learn basic programming skills.
-
BenTam 1,581 Reputation points
2022-09-15T12:56:57.393+00:00 Hi @AgaveJoe
If possible, I'll post my source code using the '101010' button. However, the code will occupy a number of characters and limit my other expressions.
Now the project was uploaded to the folder https://github.com/benyltam/NewTims. You may clone it to your Visual Studio to debug the code. -
AgaveJoe 26,151 Reputation points
2022-09-15T13:49:32.167+00:00 The solution has tons of compiler errors and does not run. Plus you have 4 different solution files. Anyway, the code base is a mess. I can't debug the code and I'm not sure where to begin.
The whole design is rather strange. You seems to put a lot of effort into creating a control library yet your data access layer is within the main project. Normally, a separate data access and business layer are the basic building blocks of any project that has a database.
In my opinion, you are trying to create a project that is far beyond your skill level. Create a single WinForms project and get the WinForms project working. Then refactor the code into class library projects.
Sign in to comment