locked
I have a variable in a new class the variable is private how can i pass iut use it in Form1 ? RRS feed

  • Question

  • private List<float> Point_X = new List<float>();
    private List<float> Point_Y = new List<float>();

    I want to create a new public function in the new class simple public function and usethe function to pass the variables so i can use them in Form1.

    Not static or anything only public.

    So when i try to use it in Form1 for example i will make in Form1:

    WireObject1.Point_X..... WireObject1 is the instance variable i did for the new class in Form1.


    danieli

    Friday, March 30, 2012 2:42 PM

Answers

  • if a member`s access modifier is marked as private, it can not be access from outside of this class where its created; in any means.

    Private is private, and its only accessible I repeat only from inside the class it was created. If it was protectred, it can be access from the derived class(es) of this one.

    So what you can do?

    You can create a public property, that can access to this private List, and it would look like:

    class WireObject
    {
        //since its private, you can SET it only inside of this class - this is what you want to right?
        private List<float> _point_X = new List<float>();
        public List<float> Point_X
        {
            get { return _point_X; }        
        }    
    
        public WireObject()
        {
    
        }
    }

    The property "Point_X" is now "read-only", this means you can access to is like you wanted to (class reference.Point_X --- NOTE: that you will not access to it with Class name directly, because its not a static property), and you cannot SET it from any where else then from WireObject class.

    Hope it helps,

    bye


    Mitja


    • Edited by Mitja Bonca Friday, March 30, 2012 3:18 PM
    • Marked as answer by chocolade Friday, March 30, 2012 3:42 PM
    Friday, March 30, 2012 3:17 PM