Solved

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

Posted on 2014-11-09
6
50 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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