Solved

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

Posted on 2014-11-09
6
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Simple Linear Regression
Introduction to Processes

724 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