المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Me, again... Confused Again...



C# Programming
12-10-2009, 03:42 AM
I thought passing an object to another object would be easy, since objects are passed by reference. Wrong.

I've managed to populate the members of an object nicely, using a series of forms for user input - that was no mean feat for me, but it works. The next step in my plan was to pass the object on to another slice of code for handling the interface to a database. If I can get that to work, it's about 75% of what the program has to do. I thought I understood that objects, as reference types, could be passed as parameters to other parts of the program, but I'm having difficulty doing so. In fact, I haven't found anything here or via Google that clearly shows anyone else trying this, so I may be completely off base.

To test my approach without getting into the database can of worms, I thought I'd test ways to do this by creating yet another form, passing the object to it, then extracting the individual members to labels for display. If that works, I can apply the same principles to a database handler module of some kind.

First I tried creating the test form with a constructor that takes one argument - my object. When I instantiated the form I passed the local object as an argument. That compiled nicely, but crashed with an unhandled null object exception. I then tried creating the form without the argument, but before displaying it, set the object member in the display form equal to the local object. Same error. I'm either trying to do something impossible, or I'm missing something important here and have no idea what it is. A partial listing follows:

Calling Form:

//Demonstrate passing object Rec to another Form object
frmShowForm frmShowRec = new frmShowForm(Rec);

if (frmShowRec.ShowDialog(this) == DialogResult.OK)
{
frmShowRec.Hide();
frmShowRec.Dispose();

Display Form:

public partial class frmShowForm : Form
{
public frmShowForm(Recloser recshow) //Test page to demonstrate passing a Ref object
{
Recloser RecShow = new Recloser();
RecShow = recshow;
label1.Text = RecShow.Serial;
this.label2.Text = RecShow.Mfr;
this.label3.Text = RecShow.Type;
this.label4.Text = RecShow.Vrated;
this.label5.Text = RecShow.BIL;

Before anyone comments, I tried the above with and without the 'this' modifier; there was no change in the error message. If either way is preferable (or necessary) I'd appreciate knowing it. This is just the latest attempt, as I've been at it all night trying different approaches to the problem and searching here and with Google. I know I could create a bunch of local variables in the calling form and pass them into the display form, but that would defeat my purpose, which is to accomplish this with as little work as possible. Typical engineer...

Thanks in advance for any assistance!

"A Journey of a Thousand Rest Stops Begins with a Single Movement"