Solved

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

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

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 74

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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.
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 …
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…

762 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

21 Experts available now in Live!

Get 1:1 Help Now