Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this post we will learn different types of Android Layout and some basics of an Android App.
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…

730 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