【问题标题】:c# How to use Delegate in method with multi returnc#如何在多返回的方法中使用Delegate
【发布时间】:2016-02-13 10:06:03
【问题描述】:

我不知道为什么这段代码不起作用。

  1. 控制台中仅显示“a=1,b=2,c=3,d=4”。
  2. myclass.a 没有工作。为什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace po
{
public delegate void godmode(int a, int b, out int c, out int d);

public class Class1
{
    public  int a;
    public int b;
    public int c;
}

public class Program
{
    Class1 myclass = new Class1();
    Program pro = new Program();
    // myclass.a didnt work


    static void go1(int a, int b, out int c, out int d)
    {
        c = a + b;
        d = (a + b) * 10;

    }
    static void go2(int a, int b, out int c, out int d)
    {
        c = a;
        d = b;

    }
    static void go3(int a, int b, out int c, out int d)
    {
        c = a * 100;
        d = b * 200;
    }


    static int outofgod(int a,out int result1,out int result2, godmode beaman)
    {
        int b = a*2;
        int c = a*3;
        int re1, re2;

        beaman(b,c,out re1,out re2);

         result1 = re1 * 10;
         result2 = re2;
    }


    static void Main(string[] args)
    {
        int a = 1;
        int b = 2;

        int c = 0;
        int d = 0;

        Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}",a,b,c,d);


        godmode mode1;
        godmode mode2;
        godmode mode3;

        mode1 = new godmode(go1);
        mode2 = new godmode(go2);
        mode3 = new godmode(go3);

        outofgod(a,out c,out d,mode1);

        Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}", a, b, c, d);

        outofgod(a, out c, out d, mode2);

        Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}", a, b, c, d);

        outofgod(a, out c, out d, mode3);

        Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}", a, b, c, d);

    }
}

}


**

myclass 不是 Class1。所以我想使用一个实例“myclass”。 但是当我输入 myclass 时,什么也没有出现。

【问题讨论】:

  • 刚刚发布的代码无法编译。如果稍微修复一下 - 工作正常。请提供minimal reproducible example(例如不需要3个版本的代表)并在每个帖子中问一个问题。
  • 使Class1 myclass = new Class1(); 静态。没有必要在它自己的程序中拥有 Program 的实例。所以删除Program pro = new Program();。无法调试,因为我不是您的调试器。也尽量不要使用无意义的名字。因为它只是令人困惑。

标签: c# visual-studio class methods delegates


【解决方案1】:

您的方法是静态的。您不能从静态方法访问非静态字段。

static void YourMethod(....) // notice static keyword
{

}

要解决这个问题,你可以将 myclass 设为静态字段。

static Class1 myclass = new Class1();

【讨论】:

    【解决方案2】:

    你可以这样使用它

    public static class Class1
    {
        public static int a;
        public static int b;
        public static int c;
    }
    

    然后您可以访问变量,如

    Class1.a
    

    因为如果你通常尝试这样做会是

    非静态字段、方法或 属性“我的班级”

    【讨论】:

      【解决方案3】:

      myClass 是一个“实例变量”,因为 Program 类不是静态的。此外,由于您的方法是静态的,您无法访问实例变量,您应该这样做:

      public class Program
      {
        Class1 myClass = new Class1();
      
        public Class1 MyClass{get{return myClass;}}
      }
      
      static void godmode()
      {
        Program p = new Program();
        var myClass = p.MyClass;
      
      }
      

      但这很糟糕。所以如果你想使用 Class1,你必须在静态方法中实例化它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-18
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-02
        • 1970-01-01
        相关资源
        最近更新 更多