2017-04-13 1 views
-1

私は派生クラスで問題があると思います。 Form 1で私のリストにいくつかのポイントを追加すると、countAを見ることができ、listAを反復することができます。それは大丈夫です。私はコードの下に持っている:私はポイントにいくつかの数学をしたいC#Deriviedクラスは値を渡しませんか?

/* 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でArgumentOutOfRangeException

class D { 
    A _myPoints = new B(); 
    public void calulations(int iterations) { 
     randomPoint = rnd.Next(0, _myPoints.listA.Count); 
     System.Windows.Forms.MessageBox.Show(_myPoints.listA.Count.ToString()); 

     try { 
      for (int i = 0; i < _iterations; i++) { 
       //here some math 
      } 
     } 
     catch(ArgumentOutOfRangeException e) { 
      // here I got errors about no items in my list 
     } 
    } } 

を得たメッセージボックスで、私は数えるだ= 0は、たぶん私が書きましたクラスは間違った方法で?メソッドdrawPoint()calculations()の前に呼び出されます。

お手伝いできますか?あなたは2つの完全に異なるオブジェクトを操作しているので、事前に感謝:)

+1

ここでは、[インスタンスメンバー](https://msdn.microsoft.com/en-us/library/aa645629(v=vs.71).aspx)に基本的な混乱があると思います。 –

答えて

1

あなたはここに

public partial class Form1 : Form 
{ 
    A _myPoints = new B(); 

を、オブジェクトを作成し、そのクラスでDは別の

class D 
{ 
    A _myPoints = new B(); 
を作成していること

最初のオブジェクトにポイントを追加し、他のオブジェクトにアクセスしようとしています。

関連する問題