非静态字段,方法或属性需要对象引用 - 错误。 [英] An object reference is required for the non-static field, method, or property -- error.
问题描述
我创建了一个XML方法,我正在尝试引用从CreateXML()的xml字符串返回到WRequest()方法,但我一直遇到以下错误 - 非静态字段,方法或属性'MailSender.createXMLPub()'需要对象引用。
I have created a XML method, and I am trying reference the return from xml string from CreateXML() into the WRequest() method, but I am keep experiencing the following error -- An object reference is required for the non-static field, method, or property 'MailSender.createXMLPub()'.
public static string WRequest(string URL, string method, string postData)
{
URL = "################";
method = "POST";
// how to read the xml data into this method
postData = createXMLPub();
string responseData = "";
try
{
if (hwrequest.Method == "POST")
{
}
// Attempt to receive the WebResponse to the WebRequest.
using (HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse())
{
if (hwresponse != null)
{ // If we have valid WebResponse then read it.
using (StreamReader reader = new StreamReader(hwresponse.GetResponseStream()))
{
reader.Close();
}
}
hwresponse.Close();
}
}
catch (Exception e)
{
responseData = "An error occurred: " + e.Message;
}
return responseData;
}
// Updated Method - currently giving new errors.
public static void createXMLPub(string data )
{
XElement xeRoot = new XElement("pub");
XElement xeName = new XElement("name", "###");
xeRoot.Add(xeName);
XElement xeCategory = new XElement("category", "#####");
xeRoot.Add(xeCategory);
XDocument xDoc = new XDocument(xeRoot);
xDoc.Save(Server.MapPath("products-string2.xml"));
//Response.Redirect("products-string2.xml");//load web browser
//return xDoc;
data = xDoc.ToString();
return data;
}
}
新错误:
的的1。方法'createXMLPub'没有重载需要0参数
2.非静态字段,方法或属性'System.Web.UI需要对象引用.Page.Server.get '
任何建议,都会受到欢迎。感谢您的帮助和时间。
New errors:
1. No overload for method 'createXMLPub' takes 0 arguments
2.An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'
Any advice, would be most welcomed. Thank you for your help and time.
推荐答案
看起来您正在从静态方法调用非静态属性。您需要将属性设为静态,
It looks like you are calling a non static property from a static method. You will need to either make the property static,
1。方法'createXMLPub'没有重载需要0个参数
答:这是你的方法
1. No overload for method 'createXMLPub' takes 0 arguments
Ans: This is your method
public static void createXMLPub(string data )
你正在使用上面这样的方法
you are using the above method like this
postData = createXMLPub();
你的参数传递给上面的方法
所以它应该是这样的
where is your parameter passing in above method
so it should be like this
postData = createXMLPub("yourdata");
2.非静态字段,方法或属性'系统需要对象引用.Web.UI.Page.Server.get'
答:在这种方法中你没有传递数据
2. An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Server.get'
Ans: In this method you are not passing data
public static void createXMLPub(string data )
并调用此方法
and calling this method at
public static string WRequest(string URL, string method, string postData)
喜欢
like
postData = createXMLPub();
这里postData是一个字符串类型,方法createXMLPub返回字符串v alue但方法签名就像void一样,所以你的createXMLPub()方法应该是这样的
here postData is a string type and the method createXMLPub is returning string value but method signature is like void so your createXMLPub() method should be like this
public static string createXMLPub(string data )
这篇关于非静态字段,方法或属性需要对象引用 - 错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!