I’m scratching my head here… I have a large program which I’ve written in C# and there is a small bug in it, which makes absolutely no sense to me.
Very quick background:
I have a class called CompoundSet which contains a DataSet (not System.Data.DataSet, it’s my own defined class) which is defined like this:
public class CompoundSet
{
private DataSet dataSet;
public DataSet { get { return dataSet; } }
//...
}
compountSet is a local member in my main workflow class, and I launch a data editor window like this:
DataWindow dw = new DataWindow(compoundSet.DataSet);
The constructor for the DataWindow is like this:
public DataWindow(DataSet dataSet)
{
// ...
this.dataSet = dataSet;
}
and the DataWindow lists the contents of the DataSet in various GUI controls.
That window shows what’s currently in the DataSet. The user can modify the data as they wish. When they close dw, the changes to the data SHOULD be in the CompoundSet since we’re only passing pointers. The DataSet in compoundSet and the DataSet in dw SHOULD be the same however it isn’t.
If I set a breakpoint in the DataWindow class in the close event, I can see that the local dw.dataSet and the WorkFlow.compoundSet.DataSet do not have the same information.
Given that I have passed a pointer to compoundSet.DataSet to the DataWindow class in the constructor, and I have nowhere used a new keyword in DataWindow, this doesn’t make any sense to me?
Why is my compoundSet.DataSet different to my dw.dataSet?
Edit: I have tried creating a DataWindow like this:
DataSet ds = compoundSet.DataSet;
DataWindow dw = new DataWindow(ds);
dw.ShowDialog(this); // breakpoint on dw.FormClosing shows the dataset has new data
return; // breakpoint here shows ds is different from dw.dataSet
Your tags are a little off (because your terminology is a little bit off) but your understanding of the concepts is correct. The code you have posted is not the source of the problem.
Specifically: you are not passing by reference, you are passing a reference type. Because it’s a reference type, you’re not passing the object itself, but a reference to the object (not a “pointer” — yes, the reference is implemented as a pointer, but that’s an implementation detail, and the word “pointer” has a specific, different meaning in c#). Because you haven’t used the “ref” keyword, you’re passing that reference by value (but this may not be relevant to your problem).
I would guess that there’s some binding problem causing the changes in the ui not to be transmitted to the dataSet object. But according to the code you’ve posted, the DataWindow and the CompoundSet should indeed be working with the same object.
It’s also possible that you’re reassigning the dataSet variable in the elided code, and expecting that reassignment to apply to the compoundSet.DataSet property, but your question implies that this is not the case.