How can i pass/use private variables froma class in Form1 with a public function?

前端 未结 5 757
粉色の甜心
粉色の甜心 2021-01-27 05:28

I don\'t want to make a new instance of Form1 in the class, and i dont want to use static.

I have these two variables which are private in the top of the cl

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-27 05:55

    There are two solutions to this:

    1. Make the variables public properties - though there are issues around the use of lists so having a ReadOnlyCollection wrapper is the way to solve this.

    2. Create a public method that performs the required manipulations on the lists.

    For example to add a point to the list you'd have:

    public void AddValueToX(float value)
    {
        PointX.Add(value);
    }
    

    If you wanted to test whether a value was in the list (which is fraught with danger as you are dealing with single precision values):

    public bool InListX(float value)
    {
        // A test on value vs the data in Point_X allowing for floating point inaccuracies
    }
    

提交回复
热议问题