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
I don't know if I understand what you need.
Anyway, you could try to use a public property:
public List<float> PointX { get { return Point_X; } }
public List<float> PointY { get { return Point_Y; } }
You can not use private fields of a class inside another (cause Form is just another class).
What you can do, if limiting the accessibility is important to you, is using internal
keyword, instead of private
. But it will work only if both classes are in the same assembly.
internal List<float> Point_X = new List<float>();
internal List<float> Point_Y = new List<float>();
To be clear it's always a good to have some property or method that "wraps" access to the private fields, but as much as I understood it's not something you want to avoid, for some reason.
There are two solutions to this:
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.
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
}
Try this:
public ReadOnlyCollection<float> GetXValues()
{
return Point_X.AsReadOnly();
}
If I understand, you want to give read-only access to Point_X
outside of the class. This method will allow you to do that. Or you could use a read-only property:
public ReadOnlyCollection<float> XValues
{
get
{
return Point_X.AsReadOnly();
}
}
The key thing is the AsReadOnly
method call to prevent changes to the collection outside of the class. If you return the List<T>
directly, it can be changed by the caller.
The canonical solution is
private List<float> mPoint_X = new List<float>();
private List<float> mPoint_Y = new List<float>();
public List<float> Point_X { get { return mPoint_X; } }
public List<float> Point_Y { get { return mPoint_Y; } }