C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,563 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
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?
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:
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.
Hi @RLWA32
Thanks for your reply. However, if I want to set the TextBox.Text to s_StudentID (now 000038, then other values), what should I do?
You need to determine why your code is using a null reference in the StudentID_textBox control.
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.
How would you use a delegate to do this? Thank You !!!
Hi @AgaveJoe and @RLWA32
Thanks for your replies. My logic is that "m_StudentID" is the int value retrieved from SQLserver. Then convert it to a string with leading '0s'. And display it in the Student_Form.
@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.
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.
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.
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.
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
Activity