how to call non static method in window console application

前端 未结 5 1185
萌比男神i
萌比男神i 2021-01-24 22:16

I built a console application and I\'m trying to test if my application works as expected.
I create an instance of the API class as shown in the code below but I receive an

相关标签:
5条回答
  • 2021-01-24 22:39

    change your method signature as follow

    public static Dictionary<String, String> getQualWeight(String sqlConStr, String inBin, Label lblResults)
    
    0 讨论(0)
  • 2021-01-24 22:41

    If you want to use an Object and not have all the members be static you need to reference the non-static member variables using an instance of the class.

    Change:

    Test_api.getQualWeight(ConStr, bin_Num, lblResults);
    

    To:

    Test_api.getQualWeight(Test_api.ConStr, Test_api.bin_Num, Test_api.lblResults);
    

    Because ConStr, bin_Num, and lblResults are instance variables they must be references with an instance of the class - in this case Test_api.

    Alternately, you could move those values into a global, static, scope by changing their declarations from:

    String ConStr = "SERVER=myservername; Database=mydb; UID=mylogin; PWD=mypassword;encrypt=no;enlist=false";
    String bin_Num = "201284-11-000";
    Label lblResults;
    

    To this:

    static String ConStr = "SERVER=myservername; Database=mydb; UID=mylogin; PWD=mypassword;encrypt=no;enlist=false";
    static String bin_Num = "201284-11-000";
    static Label lblResults;
    
    0 讨论(0)
  • 2021-01-24 22:41
     public static Dictionary<String, String> getQualWeight
    

    Put static keyword in the declaration of your method.

    0 讨论(0)
  • 2021-01-24 22:51

    You must mark ConStr, bin_Num and lblResults as static.

    static String ConStr = "SERVER=myservername; Database=mydb; UID=mylogin; PWD=mypassword;encrypt=no;enlist=false";
    static    String bin_Num = "201284-11-000";
    static    Label lblResults;
    
    0 讨论(0)
  • 2021-01-24 22:55

    You have declared the members you're passing to the function call as instance variables, but you're trying to access them from the scope of the static Main method. This is not possible, as the static Main method has no access to the member variables.

    Either declare them as static too, or use Test_API.ConStr for example as parameter to the method.

    0 讨论(0)
提交回复
热议问题