Hi all,
I've been teaching myself C# for a new project at work. I have a bit of
a background in c++ and java but never been what you could call a guru.
I'm having some strange things happening when I pass a class as a
parameter to a Windows Form. Basically, I have a class that has several
fields, two of these fields are an instance of an inner class, the rest
are basic value types (bool's in this case). I have a windows form, the
constructor takes one parameter(my class) which I then use to initialise
the controls on the form.
Now, am right in saying since my class is a reference type by default it
is passed as a reference, so any changes on the parameter by the forms
code is reflected in the calling function? This generally is what I see
happening, however I am loosing reference to my properties that are
themselves classes.
Here's a cut down version of my class:
public class PrintListConfiguration
{
public class HeaderFooterConfiguration {
public bool PerPage;
public bool Title;
public bool Criteria;
public bool Date;
public bool Pages;
#region constructors
...
#endregion
}
public bool Borders;
public bool StatusColumn;
public bool RowNumbers;
public HeaderFooterConfiguration Header;
public HeaderFooterConfiguration Footer;
#region Constructors
...
#endregion
}
As you see it has two reference fields Header and Footer.
I create a new instance of a form passing in an instance of the class as
the parameter.
PrintList printList = InitialisePrintList();
PrintListForm printListsForm = new PrintListForm(printList);
Then, in my forms constructor I set the state of the controls based on
my class I passed in.
public PrintListForm(PrintListConfiguration printList) {
InitializeComponent();
cbBorders.Checked = printList.Borders;
cbRowNumbers.Checked = printList.RowNumbers;
cbStatusColumn.Checked = printList.StatusColumn;
//cbHeader.Checked = printList.Header == null ? false : true;
//cbFooter.Checked = printList.Footer == null ? false : true;
if (printList.Header != null) {
cbHeaderDate.Checked = printList.Header.Date;
cbHeaderPages.Checked = printList.Header.Pages;
cbHeaderPerPage.Checked = printList.Header.PerPage;
cbHeaderSearchCriteria.Checked = printList.Header.Criteria;
cbHeaderTitle.Checked = printList.Header.Title;
}
if (printList.Footer != null) {
cbFooterCriteria.Checked = printList.Footer.Criteria;
cbFooterDate.Checked = printList.Footer.Date;
cbFooterPages.Checked = printList.Footer.Pages;
cbFooterPerPage.Checked = printList.Footer.PerPage;
cbFooterTitle.Checked = printList.Footer.Title;
}
}
Now, my problem:
I've commented out two tests on the classes reference fields header and
footer. It should test to see if they are null and set a checkbox state
based on that test.
If I include that test the header and footer types are lost (they just
include 5 bool values that all get reset to false). Tracing through the
code they only get set to false at that exact lines, before those lines
they contain the values passed in by the calling function.
If I move those lines to the bottom of the function then it works fine,
my forms control states are set correctly based on the passed in class.
Does anyone know what is causing this behaviour? Is there some form of
garbage collection taking place? I tried suppressing the GC but didn't
see any difference. I may have done something wrong there though as I
never persevered down that path.
I'm sure there is various other options that would result in me not
having to do what I'm doing, but now that I've gotten this far I really
what to know what's happening to my instance of my class!
Thanks in advance,
Ross McLean