2012-04-22 11 views
29

Iは、2つのクラス、アルゴリズムを実装するアルゴリズムパラメータと他を定義するための1つを有する:C#のエラー:「オブジェクト参照が非静的フィールド、メソッド、またはプロパティのために必要とされる」

クラス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; 
       } 
      } 
     } 
    } 
} 

Population[i].bits = GetRandomBits();のエラーがMain()にあります。

エラーは次のとおりです。

An object reference is required for the non-static field, method, or property 'VM_Placement.Program.GetRandomBits()'

私は何も足りませんか?

答えて

72

Mainメソッドは静的です。静的メソッドから非静的メソッドを呼び出すことはできません。

GetRandomBits() 

は静的メソッドではありません。どちらかあなたはProgram

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

のインスタンスを作成したり、

GetRandomBits()は、静的ようにする必要があります。

7

は、それはあなたが望むようになっています。あなたはGetRandomBits()メソッドを呼び出すことができます前に、

public static string GetRandomBits() 

staticがなければ、あなたはオブジェクトが必要になります。ただし、GetRandomBits()の実装はProgramオブジェクトの状態に依存しないため、staticと宣言することをお勧めします。

1

Mainメソッドは、Programクラス内では静的です。静的メソッド内からインスタンスメソッドを呼び出すことはできません。そのため、エラーが発生しています。

修正するには、GetRandomBits()メソッドも静的にするだけです。

関連する問題