472,958 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Passing a class containing reference type fields as a parameter problem.

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
Jan 10 '06 #1
3 2046
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

Jan 10 '06 #2
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.
Jan 10 '06 #3
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
Jan 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
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) {...
5
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...
0
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...
7
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...
3
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...
11
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...
6
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...
12
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...
7
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.