Non-static fields, methods, or properties require object references

Source: Internet
Author: User

encountered a C # error "Non-static field, method, or property requires object reference", initially thought to add ref or out,

The result is not a good try. Baidu easy to find is that this is because the static method of the class, the static methods in the class, member functions can only access static data members or static methods.
static void Main (string[] args)//This uses the keyword static representation to be a static method, if the outside method or function must be a static method or a function in the Main method.
In C # even static void Main (string[] args) to access the variables outside of this method must be static.

These are all in the class, and it's OK to access the other classes.
Such as: Access to other classes in the main function, you do not need to add static



The following code calls an error:"A non-static field, method, or property requires an object reference"

Private FileStream selectfilepieces (FileStream filedata, string tempfilepath, int eachlength) {
。。。。。

}


public static void Postfile (Uri requesturi, NameValueCollection postdata, String fileName,
String Filecontenttype, String filefieldname, Cookiecontainer cookies,
NameValueCollection headers,string TempFilePath)

{

FileStream f=selectfilepieces (Filedata, tempfilepath,eachlength); 。。。。。。

}

Only need to add static

private static FileStream selectfilepieces (FileStream filedata, string tempfilepath, int eachlength) {
。。。。。

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.