reference back to original value

T

TBass

I'm new to C#, and I have a question. For my program, I have 2 forms:
one to allow the user to pick a property and another to allow the user
to edit the property. They select the property they want to edit from
a TreeView.

Example:

string m_szCurrentPath;
object m_oCurrentTarget;

private void main_handleSelect( string szPath )
{

/* NUMBER OF SCREENS */
if ( szPath.CompareTo( "Basic Configuration\\Number of Screens" ) ==
0 )
{
btnEdit.Enabled = true;
/* BOX A REFERENCE TO THE VARIABLE WE WANT TO EDIT */
m_oCurrentTarget = m_szNumInputs;
m_iInputType = 0;
return;
}

/* IF WE GET HERE, THEN IT WASN'T AN EDITABLE PROPERTY */
btnEdit.Enabled = false;



} /* main_handleSelect */



private void btnEdit_Click(object sender, System.EventArgs e)
{
/* UNBOX THE VALUE TO A TEMP STRING */
string myvalue = (string)m_oCurrentTarget;
frmText mydlg = new frmText( ref myvalue, m_szCurrentPath );
DialogResult res = mydlg.ShowDialog();

if ( res == DialogResult.OK )
{
/*
* PROBLEM: THIS WON'T WORK
* OBJECT POINTS TO COPY ON THE HEAP
* I WANT TO UPDATE THE ORIGINAL VALUE.
*/
m_oCurrentTarget = myvalue;
}

}

There are 14 possible settings for the user to edit, and based off
their TreeView selection, I box a reference back to the string
variable holding that setting.

That's well and good, but when I go to update that original value, I
have a problem. The object boxing the value points to a copy on the
heap, not the original value. How do I reference and update the
original value. I'm trying to avoid using unsafe code and pointers.

Thanks!
T
 
A

Adrian

By any chance have you done a lot of C++ programming before? ;-)

Perhaps the simplest solution is pass the first form object into the
constructor for the second form, where you should assign it to a field of the
second form (in C++ terms the second form will have a reference/pointer to
the first form). Your second form will then be able to call public properties
and methods of the first form. Make the fields on the first form that you
want to be "editable" (e.g., m_szNumInputs) into public properties.

You do not need to "box" into an object - this is just confusing things and
doesn't achieve anything.
 
T

TBass

Perhaps the simplest solution is pass the first form object into the
constructor for the second form, where you should assign it to a field of the
second form (in C++ terms the second form will have a reference/pointer to
the first form). Your second form will then be able to call public properties
and methods of the first form. Make the fields on the first form that you
want to be "editable" (e.g., m_szNumInputs) into public properties.

I did something close to that. I ended up loading my properties into
an array, and then passing the array index to the second form.
Depending on the property selected, the second form is passed the
matching array index, and that index gets updated when the user
presses OK.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top