C#派生类不';t传递值?
我认为派生类有问题。当我在C#派生类不';t传递值?,c#,winforms,C#,Winforms,我认为派生类有问题。当我在表单1中向列表中添加一些点时,我可以看到count并遍历listA,这似乎没问题。我有以下代码: /* Add points to lists */ class A { private Point _singlePoint; protected List<Point> _listA = new List<Point>(); protected List<Point> _listB = new List<P
表单1
中向列表中添加一些点时,我可以看到count并遍历listA,这似乎没问题。我有以下代码:
/* Add points to lists */
class A {
private Point _singlePoint;
protected List<Point> _listA = new List<Point>();
protected List<Point> _listB = new List<Point>();
//properties
public List<Point> listA {
get { return _listA; }
set { _listA = value; }
}
public Point singlePoint {
get { return _singlePoint; }
set { _singlePoint = value; }
}
public virtual void addToListA(Point a) { }
}
class B : A {
public override void addToListA(Point a) {
_listA.Add(a);
}
}
public partial class Form1 : Form {
A _myPoints = new B();
private void drawPoint()
{
for (int i = 0; i < int.Parse(txtBoxPointsCount.Text); i++)
{
_myPoints.singlePoint = new Point(rnd.Next(100, 300), rnd.Next(100, 300));
_myPoints.addToListA(_myPoints.singlePoint);
}
foreach(Point p in _myPoints.listA)
{
graph.FillEllipse(new SolidBrush(cPoint), p.X, p.Y, 6, 6);
}
}
}
在D类中,在MsgBox中我得到count=0。也许我写错了课?方法drawPoint()
在calculations()之前调用
你能帮忙吗?提前感谢:)这是因为您正在操作两个完全不同的对象
您已经在这里创建了一个对象
public partial class Form1 : Form
{
A _myPoints = new B();
然后在D班上创造了另一个
class D
{
A _myPoints = new B();
所以,您正在向第一个对象添加点,并尝试在另一个对象中访问 我想这里有一些基本的混乱。
class D
{
A _myPoints = new B();