C# 错误:“非静态字段、方法或属性需要对象引用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10264308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# error: "An object reference is required for the non-static field, method, or property"
提问by user1277070
I have two classes, one for defining the algorithm parameters and another to implement the algorithm:
我有两个类,一个用于定义算法参数,另一个用于实现算法:
Class 1 (algorithm parameters):
第 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();
}
}
Class 2 (implement algorithm):
第 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;
}
}
}
}
}
I am getting an error on Population[i].bits = GetRandomBits();in Main().
我Population[i].bits = GetRandomBits();在Main().
Error is:
错误是:
An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()'
非静态字段、方法或属性“VM_Placement.Program.GetRandomBits()”需要对象引用
Am I missing anything?
我错过了什么吗?
采纳答案by Sandeep
The Mainmethod is Static. You can not invoke a non-static method from a static method.
该Main方法是静态的。您不能从静态方法调用非静态方法。
GetRandomBits()
is not a static method. Either you have to create an instance of Program
不是静态方法。要么你必须创建一个实例Program
Program p = new Program();
p.GetRandomBits();
or make
或制作
GetRandomBits()static.
GetRandomBits()静止的。
回答by Greg Hewgill
It looks like you want:
看起来你想要:
public static string GetRandomBits()
Without static, you would need an object before you can call the GetRandomBits()method. However, since the implementation of GetRandomBits()does not depend on the state of any Programobject, it's best to declare it static.
如果没有static,您将需要一个对象才能调用该GetRandomBits()方法。但是,由于 的实现GetRandomBits()不依赖于任何Program对象的状态,因此最好声明它static。
回答by Karl Nicoll
The Mainmethod is static inside the Programclass. You can't call an instance method from inside a static method, which is why you're getting the error.
该Main方法在Program类内是静态的。您不能从静态方法内部调用实例方法,这就是您收到错误的原因。
To fix it you just need to make your GetRandomBits()method static as well.
要修复它,您只需要使您的GetRandomBits()方法也是静态的。

