I don’t know how to call this method,
“SearchForDevices();” gives a “refrence is required for teh non-static field”
and I don’t know what that means. Please help ?
The code below is copied directky from here: https://yortw.github.io/RSSDP/
If you want to call a non-static method of an object, you need to create that object first:
If it’s a static method, you don’t need to create an object:
Doesn’t work in either case
I’m not going to bother even touching the “device discovery” aspect of what you’re trying to do. The github documentation for that package should have all the information needed to implement it.
You have not in any way given enough information for me to be sure what exactly your issue is, but based on the error message, I can make an educated guess, so if I’m off base or have guessed incorrectly, my apologies.
A typical Console application’s entry point may look like this:
If we were to modify the above to call a method in a similar manner as the example code you pasted, it might look similar to this:
CS0120: An object reference is required for the non-static field, method, or property ‘Program.MyNewMethod()’
Why? Because the application starts in Main
which is a static method. MyNewMethod
is an instance method (it’s not static) and you cannot call an instance method from a static method without first having an instance of the class available to call methods on that class. You can call static methods from other static methods. Documentation of Static Methods.
You have several options here.
Create a new class, move MyNewMethod
into that class as a public method, create a new instance of that class inside Program.Main
, and then call the method.
Make MyNewMethod
static.
(Combines #1 & #2 together) Create a new static class, move MyNewMethod
into that class as a static method, and call the method statically.
All of this is covered in early-to-moderate introductory C#, so you might want to take some time and review some of those basic concepts.
Thank you so much !
To build on what others have said, you need to make this async Task
, not async void
, as the latter cannot be awaited.
async void
should only be used when necessary (e.g. UI events).
C# devs
null reference exceptions