简体   繁体   中英

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

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 class:

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

Now in the Form1 I created a new instance for the new class: WireObject1 .

I need that in Form1 so that I will be able to type:

WireObject1.Point_X
// Or 
WireObject1.anyFunctionherethatwillcontainthePOINT_X

Same for the Point_Y.

In the class they are private, but using a public function in the class I'll be able to use them in the Form1.

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.

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
}

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; } }

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; } }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM