0

So i have this struct

public struct Way
    {
        public Int64 ID;
        public List<Int64> Ref; //contains a list with Node IDs
        public List<string> Tag;
        public List<string> Value;

        public Way(Int64 id, List<Int64> refe, List<string> tage, List<string> valuee)
        {
        ID = id;
        Ref = new List<Int64>(refe); //contains a list with Node IDs
        Tag = new List<string>(tage);
        Value = new List<string>(valuee); // This list is ALWAYS the same lenght as the Tag list
        }

    };

This struct is used as a base template to store data in retrieved from a large file. So I create a list of this struct to store all the data in.

public List<Way> WayList = new List<Way>();

In the processing algorithm i use a temporary struct to temporarily store each data in while processing the file line by line (data for 1 way can span from 4 lines to 800-1000 lines of text or more).

List<Int64> a = new List<Int64>();
List<string> b = new List<string>();
List<string> c = new List<string>();
Way FoundWay = new Way(0, a, b, c);

I then add this temporary struct into the list with the Add function.

 WayList.Add(CopyWay);

In debug mode this is what happens. The temporary struct has the correct data in it. But when i Add this struct into the WayList then this happens:

    - the ID gets input by value (each way in the list has an unique ID = good)
    - the lists gets input by reference ( each way in the list has the same lists = bad)

my question is, is there a way possible to get these list input by value, or do i need to abandon the use of a struct?

  • 2
    You probably don't want to be using a struct: http://stackoverflow.com/questions/521298/when-to-use-struct –  Jul 08 '15 at 15:34
  • 2
    That type makes no sense as a `struct`. Structs should be small, immutable, logically represent a single value, etc. This type is none of those things; it clearly ought to be a `class`. – Servy Jul 08 '15 at 15:34

1 Answers1

0

This probably shouldn't be a struct. It seems to more "naturally" be a class, though there might be a reason for using a struct that isn't obvious here (though a mutable struct is a dangerous thing, best only used very carefully and with the struct itself private or internal so that the great many complications with mutable structs don't cause problems).

Either way though:

the lists gets input by reference

Not quite. The lists get input by value, but that value is in itself a reference, because List<T> is a reference type. If you want to create a copy of a list you must do exactly that; copy the list. This holds whether the containing object is a reference or value type.

A simple way to do that would be to add a copy-constructor:

public Way (Way way)
  : this(way.ID, new List<Int64>(way.Ref), new List<string>(way.Tag), new List<string>(way.Value))
{
}

And then use it:

WayList.Add(new Way(CopyWay));
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • The way this struct is used is just to set it, and then it will purely be used to read the data from (mostly a database type). The reason for struct is basicly the way i was teached about the difference between struct and class. Struct was a grouping of different type of variables, while a class grouped the variables and also had functions to use/alter the data in some way. Anyway, this fix has helped me out, thanks for that. – Jonas De wit Jul 08 '15 at 16:00
  • Whoever taught you that difference between struct and class was very, very wrong. Or else was teaching you about a language other than C#. (If they were teaching you about C++ then they would only have been very wrong, not very, very wrong). – Jon Hanna Jul 08 '15 at 16:01
  • With C++, the difference between `struct` and `class` is that with `struct` everything is public by default and with `class` it's private by default; it's a legacy of private-by-default being the OOP preference but needing to be compatible with C. It's **common** to use `struct` as just grouping of variables and `class` for more detailed members, but there can be good reasons for exceptiosn. In C# `struct` creates a value type and `class` creates a reference type so the differences between the two are vastly different to in C++. – Jon Hanna Jul 08 '15 at 16:09