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 7718607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:12:24+00:00 2026-06-01T03:12:24+00:00

I was wondering if you could look at my code and see where I

  • 0

I was wondering if you could look at my code and see where I have gone wrong. Basically, I have created an object of type “UserFile”(my own object type) and am creating 2 instances of it and within the constructor of that object, I am calling a static class method. All goes well for them except the second instance overwrites the first one after the object constructor is called. I have stepped through the program and am thoroughly confused. I get the feeling im missing something very obvious here.

Here is the button on the form that creates the instances

    private void btnCompare_Click(object sender, EventArgs e)
    {
        if (lstFiles.CheckedItems.Count == 2)
        {
            file1 = new UserFile(((FileLocation)lstFiles.CheckedItems[0]).filePath);

            file2 = new UserFile(((FileLocation)lstFiles.CheckedItems[1]).filePath);
        }
    }

Here is the UserFile class with constructor

public class UserFile
{
    public Dictionary<int,Individual> fileIndividuals;
    public Dictionary<int, Family> fileFamilies;
    public Header fileHead;

    public UserFile(string _dir)
    {
        fileIndividuals = new Dictionary<int, Individual>();
        fileFamilies = new Dictionary<int, Family>();
        fileHead = new Header();
        ReadFromFile.Read(_dir);
        fileIndividuals = ReadFromFile.individuals;
        fileFamilies = ReadFromFile.families;
        fileHead = ReadFromFile.head;
    }
}

Here is the ReadFromFile method called by the UserFile class

static class ReadFromFile
{
    public static string filename = "";

    public static Header head;
    public static Individual individual;
    public static Dictionary<int, Individual> individuals = new Dictionary<int, Individual>();

    public static Family family;
    public static Dictionary<int, Family> families = new Dictionary<int, Family>();

    public static GedcomRecordEnum currentRecord = GedcomRecordEnum.None;
    public static GedcomSubRecordEnum currentFirstLvlRecord = GedcomSubRecordEnum.None;
    public static GedcomSecondLevelEnum currentSecondLvlRecord = GedcomSecondLevelEnum.None;

    static public void Read(string fileName)
    {
        individuals.Clear();
        families.Clear();
        head = null;
        if (File.Exists(fileName))
        {
            filename = fileName;
            StreamReader reader = new StreamReader(fileName);

            while (!reader.EndOfStream)
            {
                string currentLine = reader.ReadLine();
                Match m = Regex.Match(currentLine, "(?<index>[0-9]) (?<keyword>[A-Z_@0-9]+)(?: *)(?<detail>.*)");

                string debug = m.Groups["index"].ToString();

                switch (m.Groups["index"].ToString())
                {
                    case "0":
                        ProcessRootLevel(m.Groups["keyword"].ToString());
                        break;
                    case "1":
                        ProcessLevel1(m.Groups["keyword"].ToString(), m.Groups["detail"].ToString());
                        break;
                    case "2":
                        ProcessLevel2(m.Groups["keyword"].ToString(), m.Groups["detail"].ToString());
                        break;
                    case "3":
                        ProcessLevel3(m.Groups["keyword"].ToString(), m.Groups["detail"].ToString());
                        break;
                }
            }
            reader.Close();
        }
    }
}
  • 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-06-01T03:12:26+00:00Added an answer on June 1, 2026 at 3:12 am

    The problem is the following static properties on the ReadFromFile class, if I assume by “overwritten” you mean that both instances of UserFile point to the same data:

     public static Dictionary<int, Family> families = new Dictionary<int, Family>();
     public static Dictionary<int, Individual> individuals = new Dictionary<int, Individual>();
     public static Header head;
    

    The problem lies in the constructor of UserFile on the use of static properties.

        ReadFromFile.Read(_dir);
        fileIndividuals = ReadFromFile.individuals; // <-- Uh-oh!
        fileFamilies = ReadFromFile.families;       // <-- Uh-oh!
        fileHead = ReadFromFile.head;               // <-- Uh-oh!
    

    What happens here is the member variables fileIndividuals, fileFamilies and fileHead are set to a reference of the individuals, families and head properties on the static ReadFromFile class, not a copy (as they are classes and not value types). So the next time ReadFromFile.Read() is called the static properties on ReadFromFile are updated (overwritten), but the prior instance of UserFile just points to the same static properties, ergo file1 and file2 will have the same data.

    So how would you fix this? Two options:

    1. Make ReadFromFile and instance class, and not a static one. Construct a new instance in the UserFile constructor and don’t use any static properties.
    2. Make a copy of the data in individuals, families and head in the constructor of UserFile. “foreach” through each item, and copy it into a new dictionary.

    Simple explanation:

    When you do an assign (the = character in C#) if the object is a class, then the target is assigned a “pointer” (reference) to the right hand side. If it’s a value type it’s copied. Dictionary is a class, so you get a pointer and not a copy.

    Illustration in code:

    public static class MyStaticClass
    {
         public static List<string> MyList = new List<string> 
    }
    

    Elsewhere…

    public void MyMethod()
    {
        List<string> myList1 = MyStaticClass.MyList; 
        List<string> myList2 = MyStaticClass.MyList; 
    
        myList1.Add("Hello");  // Add to first list
        myList2.Add("World");  // Add to second list 
    
        foreach(string item in myList1) // print all items in the second list
        {
             Console.WriteLine("List 1: " + item); 
        }
    
        foreach(string item in myList2) // print all items in the second list
        {
             Console.WriteLine("List 2: " + item); 
        }
    } 
    

    The output of this will be:

    List 1: Hello
    List 1: World
    List 2: Hello
    List 2: World 
    

    But why? We only added ‘World’ to myList2. Well myList1 and myList2 point to the same thing. When we did myList1 = MyStaticClass.MyList we didn’t get a copy of the item, just a reference to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code and I was wondering if someone could look at
I'm wondering if someone could look over my code. I'm trying to pass a
I was wondering if anyone could take a look at this code and help
Hi I am wondering if someone could take a look at my code and
Hi sorry still learning here and slow to learning code arguments. Just wondering could
I was wondering if anyone could look over a class I wrote, I am
I was wondering how I could select multiple object initializers in 1 select statement
I was wondering if someone could tell me the benefits of creating a local
I'm wondering if it's possible to have something similar to ActionScript 3's Dictionary object
I have the following code grabbing a JSON object from github and I am

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.