C#错误:"需要对象引用才能访问非静态字段、方法或属性"

47

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

第一类(算法参数):

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

第二类(实现算法):

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()”要求对象引用。

我是否漏掉了什么?

3个回答

105

Main方法是静态的。你不能从一个静态方法中调用非静态的方法。

GetRandomBits()

这不是一个静态方法。你需要创建一个Program实例。

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

或者制造

GetRandomBits()是静态方法。


14

看起来您想要:

public static string GetRandomBits()

如果没有static关键字,你需要先创建一个对象才能调用GetRandomBits()方法。然而,由于GetRandomBits()方法的实现不依赖于任何Program对象的状态,所以最好将它声明为static


3

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

要修复它,您只需要将GetRandomBits()方法也定义为静态方法即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接