[英]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
. 我不想在类中创建Form1的新实例,也不想使用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
. 现在,在Form1中,我为新类创建了一个新实例: WireObject1
。
I need that in Form1 so that I will be able to type: 我需要在Form1中输入以下内容:
WireObject1.Point_X
// Or
WireObject1.anyFunctionherethatwillcontainthePOINT_X
Same for the Point_Y. 对于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. 在类中它们是私有的,但是在类中使用公共函数,我将能够在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. 据我了解,您想在类之外授予对Point_X
只读访问权限。 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. 关键是AsReadOnly
方法调用,以防止在类外部对集合进行更改。 If you return the List<T>
directly, it can be changed by the caller. 如果直接返回List<T>
,则调用者可以更改它。
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. 使变量成为公共属性-尽管列表的使用存在问题,所以使用ReadOnlyCollection
包装器可以解决此问题。
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). 您不能在另一个内部使用一个类的私有字段(因为Form只是另一个类)。 What you can do, if limiting the accessibility is important to you, is using internal
keyword, instead of private
. 如果限制可访问性对您很重要,您可以做的是使用internal
关键字,而不是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. 要明确的是,拥有一些“包装”对私有字段的访问权的属性或方法总是很好的,但是据我所知,出于某种原因,这不是您想要避免的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.