2

I have a class which defines few global variables as below:

namespace Algo
{
    public static class AlgorithmParameters
    {
        public int pop_size = 100;

    }
}

In my another csharp file, which also contains the main(), and in the main() I am declaring an array of type structure and the array size as pop_size but I am getting some error on "chromo_typ Population[AlgorithmParameters.pop_size];". Please find the code below. Am I using a incorrect syntax for array declaration of variable length size??

namespace Algo
{
    class Program
    {
        struct chromo_typ
        {
            string   bits;  
            float    fitness;

            chromo_typ() {
                bits = "";
                fitness = 0.0f;
            }

            chromo_typ(string bts, float ftns)
            {
                bits = bts;
                fitness = ftns;
            }
        };

        static void Main(string[] args)
        {

            while (true)
            {
                chromo_typ Population[AlgorithmParameters.pop_size];
            }
        }
    }
}

Error is:

Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.

Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)

Please help.

4 Answers 4

12

You don't specify the size when you declare the variable, you specify it when you create the instance of the array:

chromo_typ[] Population = new chromo_typ[AlgorithmParameters.pop_size];

Or if you separate the declaration and creation:

chromo_typ[] Population;
Population = new chromo_typ[AlgorithmParameters.pop_size];
Sign up to request clarification or add additional context in comments.

3 Comments

An object reference is required for the non-static field, method, or property 'Algo.AlgorithmParameters.pop_size'
You need to make pop_size static, eg, public static int pop_size = 100;
ohh ok I changed pop_size to static int... Thanks
2

Change the initialize in this way:

        //while (true) ///??? what is the reason for this infinite loop ???
        //{ 
            chromo_typ[] Population = new chrom_typ[AlgorithmParameters.pop_size] ; 
        //} 

also you need to change pop_size to a static because is declared inside a static class.

Comments

1

Not sure why you have to use a while(true)

But in any case, to declare array, you have to do this:

chromo_typ[] Population = new chromo_typ[AlgorithmParameters.pop_size];

and also you have to declare the member pop_size as static in AlgorithmParameters

public static class AlgorithmParameters
{
     public static int pop_size = 100;
}

1 Comment

One more thing, struct cannot have a default constructor.
0

Try to change the initialization statement as below, it will create an instance

chromo_typ[] Population = new chrom_typ[AlgorithmParameters.pop_size] ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.