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

  • Home
  • SEARCH
  • 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 8697427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:31:17+00:00 2026-06-13T01:31:17+00:00

I have found a work around for my issue, but I was wondering if

  • 0

I have found a work around for my issue, but I was wondering if someone can explain why this is happening.

I have a class DataCollector.

public class DataCollector
{
    public string Name { get; set; }

    public string Group { get; set; }

    public int Count { get; set; }

    public Brush Color { get; private set; }

    public DataCollector(string name, string group, int count, Brush color)
    {
        Name = name;
        Group = group;
        Count = count;
        Color = color;
    }
}

In my code I have an ObservableCollection of DataCollector.

public ObservableCollection<DataCollector> dataGrid { get; set; }

I collect my data and use it just fine until when I went to add more features to the code.

I want to go through “dataGrid” and make a list of Groups and get a total count from all Names.

List<DataCollector> dataCollector = new List<DataCollector>();
List<string> _groupNames = new List<string>();

foreach (DataCollector dc in dataGrid)
{
    int cnt = 0, index = 0;
    bool groupFound = false;

if (dataCollector.Count == 0)
{
    _groupNames.Add(dc.Group);
    dataCollector.Add(dc));
}
else
{
    foreach (string groupNames in _groupNames)
    {
        if (groupNames == dc.Group)
        {
            index = cnt;
            nameFound = true;
        }

        cnt++;
    }

    if (nameFound)
    {
        // When I do this my dataGrid.Count increments, too
        dataCollector[index].Count += dc.Count;
    }
    else
    {
        _groupNames.Add(dc.Group);
        dataCollector.Add(dc);
    }
}
}

To get around this I changed

dataCollector.Add(dc);

to

dataCollector.Add(new DataCollector(dc.Name, dc.Group, dc.Count, dc.Color));

Why did I have to this? Does adding “dc” to “dataCollector” create a link to “dataGrid”? If it does it doesn’t make sense.

  • 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-13T01:31:18+00:00Added an answer on June 13, 2026 at 1:31 am

    You do that because dataCollector.Add(dc) copies the reference to the memory location of an object. So changes to the data at the location is reflected in either collection as they hold pointers to the same objects.
    What you could do instead is to mimic a copy constructor.

    public DataCollector(DataCollector dataCollector)
    {
      Name = dataCollector.Name;
      Group = dc.Group;
     //...
    }
    

    It sorts of keep your code clean.

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

Sidebar

Related Questions

PLEASE NOTE, I have found a work around which actually works out better for
I have found myself doing this in my code to 'cache' the work done
I've already found a workaround to this problem, but was just wondering if anyone
Has anyone found a work around or seen the issue of admin console not
I have to start work on phone gap. Till now i have found out
I've investigated several C# DLL's and have not found any that work especially well.
I want to use the TFS WebAccess to open/manage work items. I have found
I have found Okapi Similarity measure can be used to calculated document similarity from
I have found many great answers to this from a year ago (when it
I have found this link: http://www.jfree.org/forum/viewtopic.php?f=3&t=6314 Its back in 2007 where they agree that

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.