I have an application in C# .NET which has a MainForm
and a few classes.
One of these classes receives incoming data messages from a network. I need to get these message's text appended into a multi-line textbox on the MainForm
.
I can send the message to a method in the MainForm
by making the method static, but then the static method cannot access the MainForm
's controls.
TheIncomingDataClass.cs
namespace TheApplicationName
{
class TheIncomingDataClass
{
public void IncomingMessage(IncomingMessageType message)
{
TheApplicationName.MainForm.ReceiveMSG(message);
}
MainForm.cs
public static void ReceiveMSG(string message)
{
txtDisplayMessages.AppendText(message); //This line causes compile error
}
The compile error:
An object reference is required for the nonstatic field, method, or property 'TheApplicationName.MainForm.txtDisplayMessages'
Seeing as you are new to C# I will keep this as simple as possible. You should have a Program.cs file that has a single method Main (this would have been generated by Visual Studio). You will need to make it look like the following:
Now in your incoming message you will have a way to access that form.
That method in the form would then be a instance (not static) method. E.g.
There are better ways to do it - but as a beginner I think this would be the best approach.
The difference between static and instance (instance is when you don't say static) is huge. To get to an instance method, field or property (which are collectively called members in C#) you need to have the containing instance. So:
Static are the opposite, they are the same anywhere in your application (more technically within the same AppDomain - but if you are a beginner you won't need to worry about that for a while). You don't need an instance to get to them (props to codewidgets "Static methods can access only static members"). For example:
Hopefully that gives you a good indication of what static and instance is and where to use them. The most important thing though is to try and avoid static members as best as you can - they can cause maintenance nightmares.
Microsoft has a whole write-up on the important concepts in regard to this.
Ok here goes. Static methods can access only static members. Your ReceiveMSG method is static. txtDisplayMessages is not and hence you cant access it. Why does your method need to be static? Needless to say, if you remove the static keyword that will fix your problem.
Just make ReceiveMSG part of a class, create an instance of the class and then call the method on the instance.
I think you should post the kind the solution you are expecting.
raise an event from class which the form can subscribe to.
A static method doesn't have access to members like txtDisplayMessages because it is not a part of that instance. I suggest you do some reading on the concepts of static methods and whatnot, because that is a fairly language agnostic concept. That method would best be served by removing the static modifier, because it doesn't need to be static - it appears that it would need to be called by that particular instance of that object.
Just remove the static modifier, you don't need it for your purposes. Read about statics here.