C#错误:一个对象引用是所必需的非静态字段,方法或财产 [英] C# error : An object reference is required for the non-static field, method, or property
本文介绍了C#错误:一个对象引用是所必需的非静态字段,方法或财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2班一个定义算法参数等来实现的算法:
1级(算法参数):
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间VM_Placement
{
公共静态类的AlgorithmParameters
{
公共静态INT pop_size = 100;
公共静态双crossover_rate = 0.7;
公共静态双mutation_rate = 0.001;
公共静态INT chromo_length = 300;
公共静态INT gene_length = 4;
公共静态INT max_allowable_generations = 400;
静态随机兰特=新的随机();
公共静态双random_num = rand.NextDouble();
}
}
2级(执行算法):
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间VM_Placement
{
公共类节目
{
公共结构chromo_typ
{
公共字符串位;
公众持股健身;
//公共chromo_typ(){
//位=;
//健身= 0.0;
//}
chromo_typ(字符串BTS,浮动固网)
{
位= BTS;
健身=固定电讯网络服务;
}
};
公共静态INT GetRandomSeed()
{
字节[]字节=新的字节[4];
System.Security.Cryptography.RNGCryptoServiceProvider RNG =新System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(字节);
返回BitConverter.ToInt32(字节,0);
}
公共字符串GetRandomBits()
{
串位=;
的for(int i = 0; I< VM_Placement.AlgorithmParameters.chromo_length;我++)
{
如果(VM_Placement.AlgorithmParameters.random_num> 0.5F)
比特+ =1;
,否则
比特+ =0;
}
返回位;
}
公共静态无效的主要(字串[] args)
{
随机RND =新的随机(GetRandomSeed());
,而(真)
{
chromo_typ [] =人口新chromo_typ [VM_Placement.AlgorithmParameters.pop_size]
双重目标;
Console.WriteLine(\\\
输入目标数);
目标= Convert.ToDouble(到Console.ReadLine());
的for(int i = 0; I< VM_Placement.AlgorithmParameters.pop_size;我++)
{
人口[I] .bits = GetRandomBits();
人口[I] .fitness = 0.0;
}
}
}
}
}
获取有关错误人口[I] .bits = GetRandomBits(); 。在main()
错误是:是必需的非静态字段,方法或属性VM_Placement的对象引用.Program.GetRandomBits()'
我缺少什么?
< DIV CLASS =h2_lin>解决方案
主要方法是静态的。你不能从一个静态方法调用非静态方法。
GetRandomBits()
不是静态方法。要么你必须创建程序的一个实例
程序P =新程序();
p.GetRandomBits();
或制作
GetRandomBits()
静态
I have 2 classes one for defining the algorithm parameters and other to implement the algorithm:
Class 1 (Algorithm Parameters):
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):
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;
}
}
}
}
}
Getting error on "Population[i].bits = GetRandomBits();" in main().
Error is: An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()'
Am I missing anything?
解决方案
Main method is Static. You can not invoke a non static method from a static method.
GetRandomBits()
is not a Static method. Either you have to create an instance of Program
Program p = new Program();
p.GetRandomBits();
or make
GetRandomBits()
static.
这篇关于C#错误:一个对象引用是所必需的非静态字段,方法或财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文