2014-07-21 56 views
2

我是C#的新手,我的類有這個錯誤。方法必須在非泛型靜態類中定義

Extension method must be defined in a non-generic static class 

我只是搜查,發現很多原因可以使這error..such如添加靜態類名和other..Can大家幫我看看,爲什麼這個錯誤來上課?

using System; 
using System.Collections.Generic; 
using System.Text.RegularExpressions; 

namespace LandManagment 
{ 
    class weather 
    { 

    private string xml; 
    private string FullTextXML; 
    private string PartTextXML; 
    public List<Forcast> forcastlist; 
    public Wind wind; 
    public Astronomy astronomy; 
    public Atmosphere atmosphere; 
    public Condition condition; 

    public struct Forcast 
      { 
     public Forcast(string day, string date, string low, string high, string text, string code) 
       { 
        Day = day; 
        Date = date; 
        Low = low; 
        High = high; 
        Text = text; 
        Code = code; 
       } 

       public string Day { get; private set; } 
       public string Date { get; private set; } 
       public string Low { get; private set; } 
       public string High { get; private set; } 
       public string Text { get; private set; } 
       public string Code { get; private set; } 
      } 

    public struct Wind 
    { 
     public Wind(string chill, string direction, string speed) 
     { 
      Chill = chill; 
      Direction = direction; 
      Speed = speed; 

     } 

     public string Chill { get; private set; } 
     public string Direction { get; private set; } 
     public string Speed { get; private set; } 

    } 

    public struct Astronomy 
    { 
     public Astronomy(string sunrise, string sunset) 
     { 
      Sunrise = sunrise; 
      Sunset = sunset; 


     } 

     public string Sunrise { get; private set; } 
     public string Sunset { get; private set; } 


    } 

    public struct Atmosphere 
    { 
     public Atmosphere(string humidity, string visibility, string pressure, string rising) 
     { 
      Humidity = humidity; 
      Visibility = visibility; 
      Pressure = pressure; 
      Rising = rising; 

     } 

     public string Humidity { get; private set; } 
     public string Visibility { get; private set; } 
     public string Pressure { get; private set; } 
     public string Rising { get; private set; } 

    } 

    public struct Condition 
    { 
     public Condition(string text, string code, string temp) 
     { 
      Text = text; 
      Code = code; 
      Temp = temp; 


     } 

     public string Text { get; private set; } 
     public string Code { get; private set; } 
     public string Temp { get; private set; } 


    } 

    private string GetStr(string str) { 
     try { 
      int index1; 
      int index2; 
      index1 = (str.IndexOf("<![CDATA[") + "<![CDATA[".Length); 
      index2 = str.IndexOf("]]>"); 
      string str1 = str.Substring(index1, (index2 - index1)); 
      // MsgBox(str1) 
      return str1; 
     } 
     catch (Exception ex) { 
      return "Error"; 
     } 
    } 

    private string getXML() { 
     return FullTextXML; 
    } 



     private void GetWind() { 
     int index1; 
     int index2; 
     string NewString; 
     index1 = (PartTextXML.IndexOf("<yweather:wind") + "<yweather:wind".Length); 
     NewString = PartTextXML.Substring(index1); 
     index2 = NewString.IndexOf("/>"); 
     string TodayWind= NewString.Substring(0, index2).ToLower(); 
     //speed 
     index1 = TodayWind.IndexOf("speed"); 
     string NewStr = TodayWind.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     string ss = NewStr.Substring(0, index2); 
     string[] arr = ss.Split('='); 
     string speed = FormatString(arr[1].Replace("\"", "")); 
     //direction 
     index1 = TodayWind.IndexOf("direction"); 
     NewStr = TodayWind.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string direction = FormatString(arr[1].Replace("\"", "")); 
     //chill 
     index1 = TodayWind.IndexOf("chill"); 
     NewStr = TodayWind.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string chill = FormatString(arr[1].Replace("\"", "")); 
     wind = new Wind(speed, direction, chill); 
    } 

     public void GetAstronomy() { 
     int index1; 
     int index2; 
     string NewString; 
     index1 = (PartTextXML.IndexOf("<yweather:astronomy") + "<yweather:astronomy".Length); 
     NewString = PartTextXML.Substring(index1); 
     index2 = NewString.IndexOf("/>"); 
     String TodayAstronomy = NewString.Substring(0, index2).ToLower(); 
     //sunrise 
     index1 = TodayAstronomy.IndexOf("sunrise"); 
     string NewStr = TodayAstronomy.Substring(index1); 
     index2 = NewStr.IndexOf(" am"); 
     string ss = NewStr.Substring(0, index2); 
     string[] arr = ss.Split('='); 
     String sunrise = (FormatString(arr[1].Replace("\"", "")) + "am"); 
     //sunset 
     index1 = TodayAstronomy.IndexOf("sunset"); 
     NewStr = TodayAstronomy.Substring(index1); 
     index2 = NewStr.IndexOf(" pm"); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     String sunset = (FormatString(arr[1].Replace("\"", "")) + "pm"); 
     astronomy = new Astronomy(sunrise, sunset); 

    } 


     private void GetAtmosphere() 
    { 
     int index1; 
     int index2; 
     string NewString; 
     index1 = (PartTextXML.IndexOf("<yweather:atmosphere") + "<yweather:atmosphere".Length); 
     NewString = PartTextXML.Substring(index1); 
     index2 = NewString.IndexOf("/>"); 
     string TodayAtmosphere = NewString.Substring(0, index2).ToLower(); 
     //humidity 
     index1 = TodayAtmosphere.IndexOf("humidity"); 
     string NewStr = TodayAtmosphere.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     string ss = NewStr.Substring(0, index2); 
     string[] arr = ss.Split('='); 
     string humidity = FormatString(arr[1].Replace("\"", "")); 
     //visibility 
     index1 = TodayAtmosphere.IndexOf("visibility"); 
     NewStr = TodayAtmosphere.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string visibility = FormatString(arr[1].Replace("\"", "")); 
     //pressure 
     index1 = TodayAtmosphere.IndexOf("pressure"); 
     NewStr = TodayAtmosphere.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string pressure = FormatString(arr[1].Replace("\"", "")); 
     //rising 
     index1 = TodayAtmosphere.IndexOf("rising"); 
     NewStr = TodayAtmosphere.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string rising = FormatString(arr[1].Replace("\"", "")); 


     atmosphere = new Atmosphere(humidity, visibility, pressure, rising); 

    } 

     private void GetCondition() 
    { 
     int index1; 
     int index2; 
     string NewString; 
     index1 = (PartTextXML.IndexOf("<yweather:condition") + "<yweather:condition".Length); 
     NewString = PartTextXML.Substring(index1); 
     index2 = NewString.IndexOf("/>"); 
     String TodayCondition = NewString.Substring(0, index2).ToLower(); 
     //temp 
     index1 = TodayCondition.IndexOf("temp"); 
     string NewStr = TodayCondition.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     string ss = NewStr.Substring(0, index2); 
     string[] arr = ss.Split('='); 
     string temp = FormatString(arr[1].Replace("\"", "")); 
     //code 
     index1 = TodayCondition.IndexOf("code"); 
     NewStr = TodayCondition.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string code = FormatString(arr[1].Replace("\"", "")); 
     //text 
     index1 = TodayCondition.IndexOf("text"); 
     NewStr = TodayCondition.Substring(index1); 
     index2 = NewStr.IndexOf(" "); 
     ss = NewStr.Substring(0, index2); 
     arr = ss.Split('='); 
     string text = FormatString(arr[1].Replace("\"", "")); 

     condition = new Condition(text, code, temp); 

    } 

     public void GetForecast() 
    { 
     for (int i = 0; i < 2; i++) 
     { 
      int index1; 
      int index2; 
      string NewString; 

      index1 = (NthIndexOf(PartTextXML, "<yweather:astronomy", i+1) + "<yweather:astronomy".Length); 
      NewString = PartTextXML.Substring(index1); 
      index2 = NewString.IndexOf("/>"); 
      string forc=NewString.Substring(0, index2).ToLower(); 
      //low 
      index1 = forc.IndexOf("low"); 
      string NewStr = forc.Substring(index1); 
      index2 = NewStr.IndexOf(" "); 
      string ss = NewStr.Substring(0, index2); 
      string[] arr = ss.Split('='); 
      String Low = arr[1].Replace("\"", ""); 
      //high 
      index1 = forc.IndexOf("high"); 
      NewStr = forc.Substring(index1); 
      index2 = NewStr.IndexOf(" "); 
      ss = NewStr.Substring(0, index2); 
      arr = ss.Split('='); 
      String High = arr[1].Replace("\"", ""); 
      //text 
      index1 = forc.IndexOf("text"); 
      NewStr = forc.Substring(index1); 
      index2 = NewStr.IndexOf(" "); 
      ss = NewStr.Substring(0, index2); 
      arr = ss.Split('='); 
      String text = arr[1].Replace("\"", ""); 
      //text 
      index1 = forc.IndexOf("code"); 
      NewStr = forc.Substring(index1); 
      index2 = NewStr.IndexOf(" "); 
      ss = NewStr.Substring(0, index2); 
      arr = ss.Split('='); 
      String code = arr[1].Replace("\"", ""); 
      //day 
      index1 = forc.IndexOf("day"); 
      NewStr = forc.Substring(index1); 
      index2 = NewStr.IndexOf(" "); 
      ss = NewStr.Substring(0, index2); 
      arr = ss.Split('='); 
      String day = arr[1].Replace("\"", ""); 
      //date 
      index1 = forc.IndexOf("date"); 
      NewStr = forc.Substring(index1); 
      index2 = NewStr.IndexOf(" "); 
      ss = NewStr.Substring(0, index2); 
      arr = ss.Split('='); 
      String date = arr[1].Replace("\"", ""); 

      forcastlist.Insert(i,new Forcast(day,date,Low,High,text,code)); 
     } 

    } 

     private static int NthIndexOf(this string target, string value, int n) 
    { 
     Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}"); 

     if (m.Success) 
      return m.Groups[2].Captures[n - 1].Index; 
     else 
      return -1; 
    } 


     public weather(string w, string U) { 
     System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); 
     string str = ("http://xml.weather.yahoo.com/forecastrss?w=" 
        + (w + ("&u=" + U))); 
     try { 
      // Load data 
      doc.Load(str); 
      str = doc.OuterXml; 
      FullTextXML = doc.OuterXml; 
      PartTextXML = GetStr(str); 
      this.GetAstronomy(); 
      this.GetAtmosphere(); 
      this.GetCondition(); 
      this.GetForecast(); 
     } 
     catch (Exception ex) { 
      str = "null"; 
      FullTextXML = "null"; 
      throw new Exception("Null XML"); 
     } 
    } 


    private string FormatString(String sss) { 
     char c = sss[0]; 

     c = c.ToString().ToUpper()[0]; 
     string str=""; 
     str = (c + sss.Substring(1)); 
     return str; 
    } 


    } 
} 

我試圖改變public static classExtension methods must be defined in a non-generic static class說,但得到您的幫助錯誤:( 感謝

+0

什麼是你得到了錯誤當你把「天氣」變成一個靜態類時? – DGibbs

+0

你在你的問題中包含了很多顯然不相關的代碼。您應該嘗試僅包含相關信息。 – Sayse

+0

@Sayse我不知道我的代碼在哪裏導致這個問題,,所以我分享了我所有的類.sorry –

回答

2

由於NthIndexOf是擴展方法,因此需要在public static class中定義。我懷疑你做了一個weatherpublic static class e.g:

public static class weather 
{ 
} 

然而,由於weather這不會工作包含非靜態實例成員如

private string xml; 
private string FullTextXML; 
private string PartTextXML; 
public List<Forcast> forcastlist; 

// etc... 

你應該做的是創建一個單獨的類來保存你的擴展方法,像:

public static class StringExtensions 
{ 
    public static int NthIndexOf(this string target, string value, int n) 
    { 
     Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}"); 

     if (m.Success) 
     return m.Groups[2].Captures[n - 1].Index; 
     else 
      return -1; 
    } 
} 

你會調用該從你的weather類,像這樣:

int y = PartTextXML.NthIndexOf("<yweather:astronomy", i + 1); 
+0

謝謝你..你的答案很好..但我面對一個新的錯誤..我應該問一個新的問題嗎?這是我的錯誤..錯誤自動實現的屬性'LandManagment.weather.Forcast.Code'的背景字段必須在控制返回給調用者之前完全分配。從構造函數初始化程序調用默認的構造函數 –

+0

@MajidHojati你應該開始一個新的問題,因爲這是與這個問題無關的 – DGibbs

+0

非常感謝 –

2

你必須把這個方法:

private static int NthIndexOf(this string target, string value, int n) 
{ 
    Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}"); 

    // I changed a bit your code here. 
    return m.Success ? m.Groups[2].Captures[n - 1].Index : -1; 
} 

靜態類中這是一個extension方法和所有擴展方法應該放在一個static類中,如果你調用這個調用Extensions,那麼它應該如下所示:

static public class Extension 
{ 
    public static int NthIndexOf(this string target, string value, int n) 
    { 
     Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}"); 

     if (m.Success) 
      return m.Groups[2].Captures[n - 1].Index; 
     else 
      return -1; 
    } 
} 

我們如何調用擴展方法?

input.NthIndexOf(value, n); 

其中input是我們的字符串。

欲瞭解更多關於擴展方法的信息,請看看here

+0

什麼是這種情況下的最佳解決方案?我添加了你的代碼,而不是我的舊的,我仍然得到相同的錯誤 –

+0

@MajidHojati請看我更新的帖子。我懷疑你可能不會打電話給你的擴展方法。請讓我知道結果。謝謝。 – Christos

+2

這不是你如何調用擴展方法。 – Rawling

0

您的NthIndexOf方法不在靜態類中 - 要使擴展方法起作用,它們必須在靜態類中定義。

0

1 - 按照上述正確答案的說法,將「NthIndexOf」函數移動到公共靜態類。

public static class StringExtensions 
{ 
    public static int NthIndexOf(this string target, string value, int n) 
    { 
     Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}"); 

     if (m.Success) 
      return m.Groups[2].Captures[n - 1].Index; 
     else 
      return -1; 
    } 
} 

2 - 在的getForecast功能改變,你調用該函數作爲

,而不是地方:

index1 = (NthIndexOf(PartTextXML, "<yweather:astronomy", i+1) + "<yweather:astronomy".Length); 

做到這一點:

index1 = (StringExtensions.NthIndexOf(PartTextXML, "<yweather:astronomy", i+1) + "<yweather:astronomy".Length); 
+0

謝謝我的朋友..它的工作.. –

相關問題