开发者

Where should I create objects that can be used by other class methods?

I'm not sure where to create the SolidColorBrush objects. Should they go a) inside the public MainWindow() initialization method, b) directly in the MainWindow class, or c) in a different, new method?

A few Ellipse objects are created in public MainWindow(), and SetEllipsePosition has no problem accessing them (maybe because Ellipses are passed in as an arg?). However, SolidColorBrush objects created in public MainWindow() aren't visible to SetEllipsePosition().

When I create these objects directly in the class (after 'Kinect Runtime'), SetEllipsePosition() sees them. But is this bad style?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Ellipse headEllipse = new Ellipse();
        Ellipse leftEllipse = new Ellipse();
        Ellipse rightEllipse = new Ellipse();

        SolidColorBrush greenBrush = new SolidColorBrush(Colors.Green); // where should these
        SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);     // objects be defined?
        SolidColorBrush orangeBrush = new SolidColorBrush(Colors.Orange);
        SolidColorBrush yellowBrush = new SolidColorBrush(Colors.Yellow);
    }

    //Kinect Runtime
    Runtime nui = new Runtime();


    private void SetEllipsePosition(FrameworkElement ellipse, Joint joint)
      {
      ...
                (ellipse as Ellipse).Fill = greenBrush;
                MainCanvas.Backgro开发者_运维技巧und = orangeBrush;
      ... 
      }
 }


The reason why you can't access the brushes is because they are created in the constructor MainWindow, if you but a private SolidColorBrush greenBrush; in the class itself and then in the MainWindow() add an instance like you do now you'll be able to access it in SetEllipsePosition.


Its always best practice to delcare variable in class level and instantiate it in ctor. In your case, SolidColorBrush objects are private to ctor. So whats the use mate?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜