Solved

How do I pass an object.member by reference to a function?

Posted on 2014-11-09
6
48 Views
Last Modified: 2016-06-23
Object gi has an (int) member "level".  ctrInt is a TextBox control with an (int) member "mData".  I would like changes made to the textbox to be stored in gi.level.

        ctrlInt.InitialUpdate(ref gi.m_level);

        public void InitialUpdate( ref int Data)
        {
            mData = Data;
        }
        private void ControlInt_TextChanged(object sender, EventArgs e)
        {
                int val = 0;
                if (Int32.TryParse(this.Text, out val))                 {
                    if (!((m_min <= val) && (val <= m_max)))
                    {
                          errMsg = "Please enter an integer between " + m_min + " and " + m_max;
                    }
                    else
                    {
                        mData = val;  // updating mData does not also update also update gi.level
                    }
        }

Is it possible to store the reference to an object member within a function as shown above?

Thanks,

Craig Harrington
0
Comment
Question by:cnharrington
  • 2
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40431776
No, not with value-type properties. You would have to pass the class/struct itself into the method in order for it to be updated from within the function.
0
 

Author Comment

by:cnharrington
ID: 40431805
I am concluding that there is no way the determine the "reference" address for the object.member and only the address of the object itself can be passed as an "ref" argument.  I have solved the problem by adding a delegate which provides the member updating functionality.

Thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40431852
The language itself won't permit such an action:

Screenshot
 The closest you could get would be to declare a variable outside of the method, pass that in, and then reassign the value after the function executes:

Screenshot
The reason why you cannot do what you are trying to achieve is because properties in .NET are compiled to methods behind the scenes, and you cannot pass a method by reference. Properties are merely a convenience to handling data access in .NET. At the end of the day, they are just methods.
0
 

Accepted Solution

by:
cnharrington earned 0 total points
ID: 40431874
My solution is to declare a anonymous method delegate whose purpose is to update the specific object.member.  I can then pass the delegate in the InitialUpdate argument list as follows.

    public delegate void setInt(int val);

   ctrlInt1.InitialUpdate(0, this, gi.m_level, "", delegate(int val) { gi.m_level = val; });

This provides the ctrlInt1 (textbox) a method for updating the specific object.member.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now