Click here to Skip to main content
15,558,870 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi EveryOne!

I'm having trouble accessing the contents of variable defined in another class which changes dynamically.

It is something like this
namespace CvSharp
{
    public class DetectEye
    {
        CvPoint cursorpos = new CvPoint();
                
        public IplImage DetectMyEye(IplImage sendImg)
        {
                        ......//
                        for (int i = 0; i < eyes.Total; i++)
                        {
                            CvRect r = eyes[i].Value.Rect;
                            CvPoint center = new CvPoint
                            {
                                X = Cv.Round((r.X + r.Width * 0.45) * Scale),
                                Y = Cv.Round((r.Y + r.Height * 0.45) * Scale)
                            };
                            cursorpos = center;
                            //MessageBox.Show(cursorpos.X + "\n" + cursorpos.Y);
                              ........//
                            
                    }
                    return imgROI;
                }
            }
        }
/*as cursorpos is global and I
can't have two return values in same function I'm doing this*/
        public CvPoint GetCursorPos()
        {
                return cursorpos;
        }
    }


And I'm accessing it another class as
namespace CvSharp
{
        public partial class Form1 : Form
        {
            DetectEye pupilobj = new DetectEye();
            private void BtnPowerUrEyes_Click(object sender, EventArgs e)
            {
            newcurpos = pupilobj.GetCursorPos();
            int x = newcurpos.X;
            int y = newcurpos.Y;
            MessageBox.Show("X=" + x + "\n" + "Y=" + y + "\n");
            }


I know I'm doing wrong! But don't know how to correct it :(

Please use your expertise to help me!

Thanks.
Posted
Updated 21-May-11 21:09pm
v3
Comments
Sergey Alexandrovich Kryukov 22-May-11 2:41am    
Despite of a big mistake in code, the question is not too bad, as it serves as OP uses it as an adequate tool to learn things appropriate to OP's current level of knowledge. My 4.
--SA

1 solution

First of all, you do not access the variable (field or other member) of the class. As you never use any static members here, you're trying to access a member of an instance of the class; and this is the key.

You access the instance CvPoint cursorpos correctly, but your problem is modification of it. I don't know if CvPoint a class or a structure. Is this is a structure, modification won't work. Why? Structure (struct) is a value type. When you assign newcurpose, you get a copy of your instance's data and then you modify a copy. No wonder, the data of the instance pupilobj is not modified. To make is working as is, you need to make the type CvPoint a class.

Using the class CvPoint instead of structure is one solution.
Another solution would be using structure but writing back the of the data to the pupilobj. In other words, create one more method called SetCursorPos. But don't rush. Let's make another improvement.

You should better make the CursorPos a property. You should better make is a property in all cases, even if you make CvPoint a class:

public class DetectEye {
    public CvPoint CursorPosition { get; set; } //now you can write, not only read 
} //DetectEye


or perhaps better
public class DetectEye {
    public CvPoint CursorPosition { //property
       get { return mCursorPosition; }
       set {
           mCursorPosition;
           DoSomething(); //may be you need side effect (rendering?), so setter is here for you
       }
    } //CursorPosition
    CvPoint mCursorPosition = new CvPoint(); //private member behind the property
} //DetectEye


In this way, you will be able to modify the field from the caller accessing the instance of DetectEye.

Now, couple of useful notes. Why you always use public? Public is to make access from another assembly is well. Are you sure you need it? If not, use internal instead of public, internal protected instead of protected. Just check up if you yield more access than really required.
Don't use names like "Form1", "label3". They are auto-generated and actually violate (good) Microsoft naming conventions. When you start working with such objects, always rename them to something semantic. Visual Studio re-factoring engine is great help here.

—SA
 
Share this answer
 
v4
Comments
AmarjeetAlien 22-May-11 2:57am    
Thanks for for detailed explanation!
CvPoint is a strudture
typedef struct CvPoint
{
int x;
int y;
}
CvPoint;

I'll try the modifications. Sorry for that bad naming convention.
Sergey Alexandrovich Kryukov 22-May-11 3:02am    
You're very welcome. Your clarification explains everything and confirms my fixes. My advice is to use property. As to naming, do not be sorry, just start doing it right.

As it should work for you, please formally accept my answer (green button).
Thank you.
--SA
Kim Togo 22-May-11 3:01am    
My 5.
Sergey Alexandrovich Kryukov 22-May-11 3:07am    
Thank you very much, Kim.
Nothing fantastic though: adequate question (note, that's why I up-voted it) makes the answer just to the point.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900