【问题标题】:Error when calling a method that returns a string c# [duplicate]调用返回字符串c#的方法时出错[重复]
【发布时间】:2016-03-01 09:58:12
【问题描述】:

我收到此错误,但我不知道为什么。

'非静态字段、方法或需要对象引用 属性'

为什么我需要在这里有一个对象引用?我的代码如下:

public string GetChanges()
    {
        string changelog = "";

        MySqlConnection connection = new MySqlConnection("server=127.0.0.1;uid=root;pwd=pass;database=data");
        try
        {
            connection.Open();

            MySqlCommand cmd = new MySqlCommand("SELECT `change_log` FROM version WHERE ID = '1'", connection);

            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (!reader.IsDBNull(0))
                {
                    changelog = reader.GetString(0);
                }
            }

            connection.Close();
        }
        catch
        {
            //MessageBox.Show(e.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        return changelog;
    }

我这样调用上面的函数:

string changelog = GetChanges();

为什么在这种情况下需要对象引用?我不能使用静态,因为我正在创建一个不适用于静态方法的 Web 服务。如何更改它以使用对象?

谢谢

【问题讨论】:

  • 你从哪里打电话给getChanges()
  • 您的GetChanges 方法默认情况下是一个实例方法。如果没有所属的类型名称,您无法调用它。将其设为static 或使用string changelog = MyType.GetChanges(); 之类的类型调用它
  • 你在哪里调用 GetChanges 方法?在我看来,您应该将方法的逻辑与 Web 服务接口分开,并从代码中调用逻辑而不是服务。直接调用 Web 服务方法有异味 - 您基本上是在试图捏造接口。
  • 我发布的代码实际上还没有在我的 Web 服务中。我最初是通过在 winforms 项目的 Program.cs 中使用该方法来测试它,并从那里调用它。但是现在我将该方法放在我的 Web 服务中,并从 winforms 项目中调用它,对象似乎没有问题。出于好奇 Luaan,为什么直接调用 web 方法会有异味?我在 web 方法中有上述函数,并从另一个项目中调用它,如下所示:MyService.GetChanges();创建 WebService 对象后。我是这样直接调用的吗?

标签: c#


【解决方案1】:

由于您的GetChanges 不是static 方法,因此如果没有具有该方法的object 实例,则无法调用它:

public class MyClass {
    public string GetChanges(){
        ....
        return str;
    }
}

那么你可以这样称呼它:

MyClass insMyClass = new MyClass(); //note the instance of the object MyClass
string str = insMyClass.GetChanges();

或者,如果你声明为static,你必须使用类名来调用:

public static string GetChanges(){ //note the static here
    ....
    return str;
}

这样称呼它:

string str = MyClass.GetChanges(); //note the MyClass is the class' name, not the instance of the class

只有在类本身中调用GetChanges,才可以:

public class MyClass {
    public string GetChanges(){
        ....
        return str;
    }

    public void Foo(){
        string somestr = GetChanges(); //this is ok
    }
}

【讨论】:

    【解决方案2】:

    您不能从static 方法调用非static 方法。

    您需要从声明 GetChanges() 的类中实例化一个对象。

    Foo f = new Foo();
    f.GetChanges();
    

    【讨论】:

      【解决方案3】:

      您的通话仅在您的班级内正常。在它之外,不是。

      您必须将其定义为:

      public class MyClass{
          public static string GetChanges(){...}
       }
      

      然后调用它

      MyClass.GetChanges();
      

      或创建包含 GetChanges 方法的类的实例。

      例子:

      public class MyClass
          public string GetChanges(){....}
      

      然后

      var mc = new MyClass();
      mc.GetChanges();
      

      【讨论】:

        猜你喜欢
        • 2011-03-12
        • 2011-04-14
        • 2020-01-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-25
        • 1970-01-01
        • 2016-07-23
        • 1970-01-01
        相关资源
        最近更新 更多