简体   繁体   English

方法必须在非泛型静态类中定义

[英]method must be defined in a non-generic static class

I am new with C#, I have this error with my class. 我是C#的新手,我的课上有这个错误。

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

I just searched and found many reasons that could make this error..such as adding static to class name and other..Can you help me find why this error comes for class? 我只是搜索并找到了许多可能导致此错误的原因...例如将静态添加到类名和其他..可以帮助我找到为什么这个错误来自类?

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;
     }


    }
}

I tried to change public static class as Extension methods must be defined in a non-generic static class said but got error :( thanks for your help 我试图更改public static class因为扩展方法必须在非泛型静态类中定义但是有错误:(感谢您的帮助

Since NthIndexOf is an extension method, it needs to be defined in public static class . 由于NthIndexOf是一个扩展方法,因此需要在public static class定义。 I suspect you've made weather a public static class eg: 我怀疑你已经把weather变成了public static class例如:

public static class weather
{
}

However, this wont work since weather contains non-static instance members eg 然而,这不会起作用,因为weather包含非静态实例成员,例如

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

// etc...

What you should do is create a separate class to store your extension method in, something like: 你应该做的是创建一个单独的类来存储你的扩展方法,如:

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;
    }
}

You would call this from your weather class like so: 您可以从weather等级中调用此方法,如下所示:

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

You have to place this method: 你必须放置这个方法:

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;
}

inside a static class. 在静态类中。 This is an extension method and all extensions methods should be placed inside a static class. 这是一个extension方法,所有扩展方法都应放在static类中。 If you call this calls Extensions , then it should be like below: 如果你打电话给这个叫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;
    }
}

How do we call an extension method? 我们如何调用扩展方法?

input.NthIndexOf(value, n);

where input is our string. input是我们的字符串。

For more information about extension methods, please have a look here . 有关扩展方法的更多信息,请查看此处

您的NthIndexOf方法不在静态类中 - 要使扩展方法起作用,必须在静态类中定义它们。

1 - Do as the above correct answers tell, move "NthIndexOf" function to a public static class. 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 - In GetForecast function change the place where you call the function as 2 - 在GetForecast函数中,将您调用函数的位置更改为

instead of: 代替:

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

do this: 做这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM