user5765700
user5765700

Reputation:

Loop not running when its method to run it is called

If I call the default constructor "Creature" in main and then try to call the method "Generate Creator" the loop never runs. If I did a normal for loop it throws out of bounds errors even knowing the default constructor sets the length of the array. It is probably a dumb error I am not seeing. (this isnt all of the code)

        class Creature
        {
           static int geneLength;
           byte[] Chromosome = new byte[geneLength];
           int fitness;

           string geneString;
           Random rn = new Random();

           public Creature()
           {
              geneLength = 36;
              fitness = 0;
           }

           public void GenerateCreature()
           {
              foreach(byte g in Chromosome)
              {
               Chromosome[g] = (byte)rn.Next(0, 2);
               geneString += Chromosome[g].ToString();
              }
           }
        }

Main:

       namespace Creature_Generator
       {
          class Program
          {
             static void Main(string[] args)
             {
                Creature c = new Creature();

                c.GenerateCreature();
                Console.WriteLine(c.getGeneString);

             }
          } 
       }

Upvotes: 0

Views: 111

Answers (3)

Enigmativity
Enigmativity

Reputation: 117010

If you want geneLength to be a static field then you need to initialize it with a static constructor. Otherwise the value of geneLength is still 0 at the moment you instantiate the class.

Write your code like this instead:

class Creature
{
    static int geneLength;
    byte[] Chromosome = new byte[geneLength];
    int fitness;

    string geneString;
    Random rn = new Random();

    static Creature()
    {
        geneLength = 36;
    }

    public Creature()
    {
        fitness = 0;
    }

    public void GenerateCreature()
    {
        foreach (byte g in Chromosome)
        {
            Chromosome[g] = (byte)rn.Next(0, 2);
            geneString += Chromosome[g].ToString();
        }
    }
}

Upvotes: 0

Zay Lau
Zay Lau

Reputation: 1864

foreach(byte g in Chromosome)
{
    Chromosome[g] = (byte)rn.Next(0, 2); // 'g' is not an index
    geneString += Chromosome[g].ToString(); // 'g' is not an index
}

while you are using foreach (byte g in Chromosome), I believe it is not a proper way to use code like Chromosome[g] which g is suppose a value not an index

try

StringBuilder geneString = new StringBuilder();
public Creature()
{
    geneLength = 36;
    this.Chromosome = new byte[geneLength];
    fitness = 0;
}

for (int i = 0; i < this.Chromosome.Length; i++)
{
    byte g = this.Chromosome[i]; // this line is useless
    this.Chromosome[i] = (byte)rn.Next(0, 2);
    geneString.Append(this.Chromosome[i].ToString());
}

Plus, if you are hard-coding geneLength = 36 in the constructor, consider use public int geneString { get; private set;} instead of static int geneLength; or

static int _geneLength = 36;
public int geneLength { get; private set; }

public Creature() { this.geneLength = _geneLength; }
public Creature(int geneLength) { this.geneLength = geneLength; }

Edit 1 -

According to @moreON advice, string geneString is modified from class string to StringBuilder, read more on https://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.110).aspx

Upvotes: 2

moreON
moreON

Reputation: 2008

Other than your issue attempting to use the results of foreach to index an array, as others have mentioned, you're also ignoring the order in which initialisers and constructors are executed.

If we ignore inheritance (because you have none that is interesting here). Initializers are run before constructors.

This means that you are creating the array Chromosome before assigning 36 to geneLength. This means that geneLength was still default(int), which is 0, so you created an array with length 0.

For more on c# constructors see Jon Skeet's excellent page: http://jonskeet.uk/csharp/constructors.html

geneLength should probably also not be static, but that's a different discussion.

Upvotes: 2

Related Questions