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 3 2076
Ross McLean wrote: 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.
That's correct. Unfortunately, you haven't shown us enough code to see
what's going wrong. In particular, you haven't shown the constructors
which are meant to set the header and footer information.
Could you post a short but complete program that demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.
(Just a slight aside - you haven't currently got properties, you've got
fields which are public. That's generally not a good idea - it's better
to expose a public property which has an underlying private field
backing it.)
Jon
Jon Skeet [C# MVP] wrote: That's correct. Unfortunately, you haven't shown us enough code to see what's going wrong. In particular, you haven't shown the constructors which are meant to set the header and footer information.
Could you post a short but complete program that demonstrates the problem?
See http://www.pobox.com/~skeet/csharp/complete.html for what I mean by that.
Jon
Thanks for the reply Jon. I'll constructor a working simplified working
sample and post.
I'd removed the constructors as in this instance the object is being
serialized using the XmlSerailizer so there I didn't bother showing it.
(Just a slight aside - you haven't currently got properties, you've got fields which are public. That's generally not a good idea - it's better to expose a public property which has an underlying private field backing it.)
Yep, absolutely correct, I do have fields, not properties. At the time
of creating pecific class I was playing with XmlSerialiser and didn't
know that if it would serialise public properties. Thanks for the reply Jon. I'll construct simplified working sample and post.
Well in creating a simplified version of my code I can't recreate the
problem! It does however give me some further avenues to explore.
The sample program did not involve any Forms, just straight method
parameter passing, and worked as I expected.
I'm running Vis Studio 2005 Beta .Net Framework 2.0 so I will retry my
actual code where possible using .Net 1.1.
Thanks,
Ross McLean This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: domeceo |
last post by:
can anyone tell me why I cannot pass values in a setTimeout function
whenever I use this function it says "menu is undefined" after th
alert.
function imgOff(menu, num) {
if (document.images) {...
|
by: Andy |
last post by:
Hi
Could someone clarify for me the method parameter passing
concept?
As I understand it, if you pass a variable without
the "ref" syntax then it gets passed as a copy.
If you pass a...
|
by: Dot Netizen |
last post by:
I am having trouble passing an ADODB.Recordset from a VB6 application
to a VB.Net class library using COM Interop. I am running this on XP
SP2 with the .Net Framework 1.1 and MDAC 2.8 SP1.
I've...
|
by: Ken Allen |
last post by:
I have a .net client/server application using remoting, and I cannot get the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException...
|
by: Mark |
last post by:
Hi
From what I understand, you can pass arrays from classic ASP to .NET using
interop, but you have to change the type of the.NET parameter to object.
This seems to be because classic ASP passes...
|
by: John Pass |
last post by:
Hi,
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line...
|
by: MSDNAndi |
last post by:
Hi,
I get the following warning:
"Possibly incorrect assignment to local 'oLockObject' which is the argument
to a using or lock statement. The Dispose call or unlocking will happen on
the...
|
by: scottt |
last post by:
hi,
I am having a little problem passing in reference of my calling class (in my
..exe)into a DLL. Both programs are C# and what I am trying to do is pass a
reference to my one class into a DLL...
|
by: TS |
last post by:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |