views:

402

answers:

2

Hi,

I am working on some software that should be used for a special type of experiment.

The experiments are performed using:

1) A "Chip" (basically an XY grid of known dimensions). 2) Each Chip contains "Electrodes", identified by their X and Y coordinate on the chip and by a unique ID. Each electrode can also hold or not hold a sample, indicated by a simple bool (it's a biosensing chip).

I have objects that represent this hardware in C#.

I now need to use the hardware in an experiment;

1) I have an "Experiment" which exposes an IEnumerable holding "ExperimentStep" objects. 2) An "ExperimentStep" holds a name, and a limited list of "Electrodes" involved among other things.

Some experiment steps could run concurrently and could change the "HasSample" property of an electrode. Therefore, when I perform an "ExperimentStep" it might be good to know what the initial "HasSample" property is at any one time.

This is where my problem lies;

If I just pass "Electrode" objects to my "ExpermentStep" they will probably be passed by Value... Is it possible to create an IEnumerable that holds references to the unique electrodes so that each time I want to run an "ExperimentStep" the list of "Electrodes" used in that step holds the most recent value for "HasSample"? Should I use pointers for this?? From my limited knowledge of C++ I would expect that this would be trivial in that language (since you work with pointers most of the time). But in C# I really have no clue (and I am not experienced enough).

+3  A: 

I suspect you don't understand the difference between reference types and value types, and what pass by value really means in C#.

Assuming Electrode is a class, you can modify the properties of an instance of it and those changes will be visible via any reference to the same object.

I strongly recommend you make sure you have a firm understanding of the .NET type system before trying to develop a lot of production code. The consequences of not understanding what's going on can be disastrous.

A couple of my articles on these topics:

... but I suggest you get a good introductory C# book as well.

Jon Skeet
Well... although I am certainly not an expert programmer I already wrote my fair share of apps (some of them even use design patterns like Singletons, Factories, ...!! OMG) Yet until now I never came into a situation where the stuff I asked about seemed important. If I have any gripes with C# (or by extension .NET) at all then it would be that it never really forces you to think about these things properly (although it maybe should).
Kris
And to go even further; I think this quote from your second link sums it up quite nicely (when talking about passing value types by ref as opposed to ref objects by value):"This difference is absolutely crucial to understanding parameter passing in C#, and is why I believe it is highly confusing to say that objects are passed by reference by default instead of the correct statement that object references are passed by value by default."What you do when passing an object by value to another class is in fact passing a typesafe pointer to that object...
Kris
None of the nice introductory books really emphasize this properly (I have the one by Mayo)
Kris
I think C# 3.0 in a Nutshell does a reasonable job, IIRC.
Jon Skeet
+2  A: 

In c# a class is reference type. This means that if you create list of instances of a class and then also add the instances to another list then its the same items. Each list will hold a reference. So to that effect yes you can up the items using an IEnumberable.

Preet Sangha