【问题标题】:C# error: "An object reference is required for the non-static field, method, or property"C# 错误:“非静态字段、方法或属性需要对象引用”
【发布时间】:2012-05-03 02:32:59
【问题描述】:

我有两个类,一个用于定义算法参数,另一个用于实现算法:

1 类(算法参数):

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

namespace VM_Placement
{
    public static class AlgorithmParameters
    {
        public static int pop_size = 100;
        public static double crossover_rate = 0.7;
        public static double mutation_rate = 0.001;

        public static int chromo_length = 300;
        public static int gene_length = 4;
        public static int max_allowable_generations = 400;

        static Random rand = new Random();
        public static double random_num = rand.NextDouble();
    }
}

第 2 类(实现算法):

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

namespace VM_Placement
{
    public class Program
    {
        public struct chromo_typ
        {
            public string   bits;
            public float    fitness;

            //public chromo_typ(){
            // bits = "";
            // fitness = 0.0f;
            //}
            chromo_typ(string bts, float ftns)
            {
                bits = bts;
                fitness = ftns;
            }
        };

        public static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            System.Security.Cryptography.RNGCryptoServiceProvider rng =
              new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);
        }

        public string GetRandomBits()
        {
            string bits="";

            for (int i = 0; i < VM_Placement.AlgorithmParameters.chromo_length; i++)
            {
                if (VM_Placement.AlgorithmParameters.random_num > 0.5f)
                    bits += "1";
                else
                    bits += "0";
            }
            return bits;
        }

        public static void Main(string[] args)
        {
            Random rnd = new Random(GetRandomSeed());

            while (true)
            {
                chromo_typ[] Population = new chromo_typ[VM_Placement.AlgorithmParameters.pop_size];
                double Target;

                Console.WriteLine("\n Input a target number");
                Target = Convert.ToDouble(Console.ReadLine());

                for (int i = 0; i < VM_Placement.AlgorithmParameters.pop_size; i++)
                {
                    Population[i].bits = GetRandomBits();
                    Population[i].fitness = 0.0f;
                }
            }
        }
    }
}

我在Main() 中的Population[i].bits = GetRandomBits(); 上遇到错误。

错误是:

非静态字段、方法或属性“VM_Placement.Program.GetRandomBits()”需要对象引用

我错过了什么吗?

【问题讨论】:

    标签: c# non-static


    【解决方案1】:

    Main 方法是静态的。不能从静态方法调用非静态方法。

    GetRandomBits()
    

    不是静态方法。要么你必须创建一个Program的实例

    Program p = new Program();
    p.GetRandomBits();
    

    或制作

    GetRandomBits()静态。

    【讨论】:

      【解决方案2】:

      看起来像你想要的:

      public static string GetRandomBits()
      

      没有static,您需要一个对象才能调用GetRandomBits() 方法。但是,由于GetRandomBits() 的实现不依赖于任何Program 对象的状态,因此最好将其声明为static

      【讨论】:

        【解决方案3】:

        Main 方法在 Program 类中是静态的。您不能从静态方法内部调用实例方法,这就是您收到错误的原因。

        要修复它,您只需要将 GetRandomBits() 方法也设为静态。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多