Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

reference back to original value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TBass - 22 Feb 2008 15:44 GMT
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
Adrian - 22 Feb 2008 17:34 GMT
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.

> 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
[quoted text clipped - 55 lines]
> Thanks!
> T
TBass - 22 Feb 2008 20:12 GMT
> 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!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.