How to show the contents of a variable that is in the class library in a form or console?

I have a C # solution that contains a project and a class library written by someone else. The class library receives GPS data via Wi-Fi. The project shows the processed data on the map. Now I want to see the contents of some variables in the class library in real time for debugging purposes.

I tried to use

System.Diagnostics.Debug.Write(value) 

without success. He seems to be doing nothing.

I also tried "MessageBox.Show (value.ToString ())", which was good, but it stopped reading Wi-Fi and interrupted the program, because I had to press the OK button every time it appeared.

In addition, I added a window shape (containing a text block) to the form library to print data in a text field, however, when I wrote:

 Form1.textBox1.Text = value.ToString() 

This gives me this error:

Error 3 "MapNMEA.Form1.textBox1" is not available due to the protection level C: \ Users \ john \ Documents \ Visual Studio 2010 \ Projects \ new ver of map purecom \ MapExplorer \ MapExplorer \ MapNMEA \ MapNMEA \ SerialPort. cs 184 27 MapNMEA

"MapNMEA" is the name of the class library. Also, "MapExplorer" is the name of both the solution and the project. "SerialPort.cs" is the file inside the class library in which I wrote the code above.

My question is how to show the contents of this variable (these are 3.4 variables) in real time, and this act should not stop reading Wi-Fi. I need to do this as an electrician who knows little about programming, wants to check whether the GPS device sends data correctly to my computer or not.

UPDATE 1 . In fact, I noticed that System.Diagnostics.Debug.Write(value) works, but since there were too many warning messages in the debug window, I did not notice this. Now, if somehow I can delete (or hide) other (warning) messages, I will only see my desired result. Did you know?

+4
source share
4 answers

Debug.Write should be fine if you attach the listener first ( Debug.Listeners.Add( new _a_listener_type() ) ).

In addition, you should probably know the AutoFlush property in the Debug class, which determines whether Flush will be called automatically.

+1
source

Debug.Write should work - by default it will be written to the Debug window in Visual Studio if you have a debugger. Are you sure you are looking in the right place?

If you want to use the form approach, you need to track the instance of the open form and provide it with a public method. For instance:

 public void WriteDebug(string message) { TextBox1.Text += message + Environment.NewLine; } 

Then you can call formInstance.WriteDebug(message); .

+1
source

I think you cannot find the right location: is there something that prevents you from debugging on the PC before moving on to the goal? If not, you should probably use the traditional way: put spyware on variables, use your IDE (Visual Studio) to view them, etc.

If you REALLY NEED to run your target audience without advanced debugging tools, you may need some simple solutions:

  • write them to a text file (add or replace what you need), then the viewer will open
  • create another modeless form with a text field and call form2.writeDbgTextBox (String) every time you need to update

Be sure to remove this code upon release (for example, by placing them in the #if DEBUG section)

And no matter what happens, DO NOT try to write to an existing message box! they are created to pop up and close, and not interact with your code.

0
source

Are you starting a debug build? Also your code with a text box does not work, because textBox1 not public

0
source

Source: https://habr.com/ru/post/1388523/


All Articles