Grasshopper

algorithmic modeling for Rhino

Following on from a previous discussions - still getting my bearings around C# modules & their limitations within GH...

I'm passing a DataTable object from one module to another, attempting to copy and modify it in the process. The DataTable is populated with custom "Cell" objects, and each module can access Cell types by referencing a compiled DLL (code below):

namespace DataTableCell
{
    public class Cell
    {
        public string Row { get; set; }
        public int Period { get; set; }
        public double Area { get; set; }
        public double Rate { get; set; }
        public double Trend { get; set; }
        public double Value { get; set; }

        private Cell(string row, int period, double area, double rate, double trend, double value)
        {
            Row = row;
            Period = period;
            Area = area;
            Rate = rate;
            Trend = trend;
            Value = value;
        }
    }
}

When I modify a cell object and a string in a copy of the DataTable (in a separate module) however, it looks like the Cell object in the "unmodified" DataTable is modified too, even though the string object isn't...

I have a feeling this is to do with C# and its DataTable.Copy() method, even though it says it copies the data? Or is it something to do with how I've defined my Cell class?

Views: 717

Attachments:

Replies to This Discussion

Looks like you might need either Clone() instead of Copy(), or maybe even both. I haven't tried (my potatoes are boiling!).

https://www.codeproject.com/Articles/1037138/DataTable-Copy-Vs-Data...

Thanks for the quick reply David. I'm pretty sure I'm looking for Copy() - as I want to modify the data rather than re-create it.

The core to the issue is that in modifying the .Copy() version of the DataTable, some of the data changes do not propagate to the original DataTable (the string "foo" doesn't appear in the unmodified result), but that data changes to an instance of my custom Cell class do propagate to the original DataTable instance (dtCopy.Rows[3][1].Rate = 1000000 in both modified and original instances...), leading me to think it's something to do with the definition of the Cell class and public/private/protected etc access modifiers maybe?

If you have your own reference types in a cell, then the DataTable probably doesn't know how to copy it, so it just sticks the same instance into the copied table. Strings are immutable objects, once made they cannot be changed. Maybe that will also be a good solution for you?

Thanks Tom & David - yes it seems the Copy() method for DataTables does not know how to deep copy my custom Cell class (and I am also learning/stumbling through C#'s idiosyncrasies). The solution was to write a copy method into the class that serialized and deserialized an object instance to form a new one...

As for the DataTable, I'm looking for a way to easily manipulate and filter multiple spreadsheets, and it seemed like the DataTable class provided a few nice methods (like DataViews, SQL loading & querying etc). The idea behind having a custom Cell class was to be able to track those manipulations/filters on the data... But I'm open to other suggestions! (and yes - cell being private was part of me testing..!)

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service