none
An object reference is required for the non-static field, method, or property RRS feed

  • Question

  • using System;
    using InfluxDB.Collector;
    using InfluxDB.LineProtocol;
    using System.Diagnostics;
    using System.ComponentModel;
    using System.Threading.Tasks;
    using System.Collections.Generic;

    namespace <g class="gr_ gr_20 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="20" id="20">influxtest</g>
    {
        class Program
        {
           static  void Main(string[] args)
            {
                Collect().Wait();


            }
            async Task Collect()
            {
                var process = Process.GetCurrentProcess();
                Metrics.Collector = new CollectorConfiguration()
                   .Tag.With("host", Environment.GetEnvironmentVariable("COMPUTERNAME"))
                   .Tag.With("os", Environment.GetEnvironmentVariable("OS"))
                   .Tag.With("process", System.IO.Path.GetFileName(process.MainModule.FileName))
                   .Batch.AtInterval(TimeSpan.FromSeconds(2))
                   .WriteTo.InfluxDB("http://192.168.99.100:8086", "data")
                   .CreateCollector();

                while (true)
                {
                    Metrics.Increment("iterations");

                    Metrics.Write("cpu_time",
                        new Dictionary<string, object>
                        {
                            { "value", process.TotalProcessorTime.TotalMilliseconds },
                            { "user", process.UserProcessorTime.TotalMilliseconds }
                        });

                    Metrics.Measure("working_set", process.WorkingSet64);

                    await Task.Delay(1000);
                }
            }
        }
    }

    Hello, I am new to c sharp I have an error in the Collect() method, the debugger display: An object reference is required for the non-static field, method, or property. where I made the mistake how I can resolve this error.

    thank you in advance.

                          
    Monday, March 18, 2019 6:56 PM

Answers

  • The problem is that Collect is invoked from Main. However, Main is static and Collect is not static. That's why you are getting the error.

    The easiest solution would be to add the "static" keyword to the declaration of Collect. However, this would again cause the same problem if Collect calls something else that is not static.

    The alternative is to create an instance of the class and call collect on the instance:

             static  void Main(string[] args)
             {
                 var instance = new Program();
                 instance.Collect().Wait();
             }

    • Proposed as answer by DerChris88 Monday, March 18, 2019 9:53 PM
    • Marked as answer by salimi.NET Wednesday, October 9, 2019 6:18 PM
    Monday, March 18, 2019 7:55 PM
    Moderator

All replies

  • The problem is that Collect is invoked from Main. However, Main is static and Collect is not static. That's why you are getting the error.

    The easiest solution would be to add the "static" keyword to the declaration of Collect. However, this would again cause the same problem if Collect calls something else that is not static.

    The alternative is to create an instance of the class and call collect on the instance:

             static  void Main(string[] args)
             {
                 var instance = new Program();
                 instance.Collect().Wait();
             }

    • Proposed as answer by DerChris88 Monday, March 18, 2019 9:53 PM
    • Marked as answer by salimi.NET Wednesday, October 9, 2019 6:18 PM
    Monday, March 18, 2019 7:55 PM
    Moderator
  • Thank you so much, Mr for your answer. it works for me. now I understand the modifier static !
    Tuesday, March 19, 2019 4:56 PM
  • Hi

    It seems that your problem has been solved. If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 20, 2019 1:18 AM
    Moderator