Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:26:58+00:00 2026-05-26T20:26:58+00:00

I have filled an array of this object and I think I’m filling it

  • 0

I have filled an array of this object and I think I’m filling it fine until I ask to repeat what I have just filled it with and see that they are all the same result. I know it’s something simple but I’m missing it right now.

static void Main(string[] args)
{
    int numOFItems = 6;
    int population = 10;

    int[] geneList = new int[numOFItems];
    Genome[] gen = new Genome[population];
    Random rand = new Random();
    Genome gene;

    for (int i = 0; i < population; i++)
    {
        gen[i] = new Genome();
        for (int j = 0; j < numOFItems; j++)
        {
            geneList[j] = rand.Next(0, 4);
        }
        gene = new Genome(geneList);
        gen[i] = gene;
        Console.Out.Write("\n" + gen[i].ToString());
    }
    for (int i = 0; i < population; i++)
        Console.Out.Write("\n" + gen[i].ToString() + ";");
    Console.ReadLine();
}

class Genome
{
    int[] geneList;
    int numOFItems = 6;

    public Genome()
    {
        geneList = new int[numOFItems];
    }

    public Genome(int[] geneList)
    {
        this.geneList = geneList;
    }

    public int[] GeneList
    {
        get { return geneList; }
        set { geneList = value; }
    }

    public override string ToString()
    {
        return "" + GeneList[0] + GeneList[1] + GeneList[2] 
            + GeneList[3] + GeneList[4] + GeneList[5];
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:26:59+00:00Added an answer on May 26, 2026 at 8:26 pm

    You’re only initializing geneList once – so each Genome contains a reference to the same array. Move your declaration and initialization of geneList inside the loop. Note that you’re also creating a new Genome instance right at the start of the list, and overwriting it afterwards. So I think your loop ought to look like this:

    for (int i = 0; i < population; i++)
    {
        int[] geneList = new int[numOFItems];
        for (int j = 0; j < numOFItems; j++)
        {
            geneList[j] = rand.Next(0, 4);
        }
        gen[i] = new Genome(geneList);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one array that is filled by mysql_query. Values that are in this
I have a user a object that looks like this. $user = new User();
I have one array that is pre-filled with dictionaries with each dictionary consisting of
I have an array filled with strings that have the same name as Google
I have dynamic array filled with bytes, which are read from .raw file with
I have a byte array filled from a file uploaded. But, in another part
I have a byte array filled with hex numbers and printing it the easy
I have 3 arrays of data, that are filled by reading off of an
I have two arrays, @names and @employees , that are filled with strings that
I have filled List<Object1> Object 1 contain 2 fields (int id & string name)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.