2015-01-20 64 views
-1

对于C#和OOP来说相当新手我在范围和访问方面遇到了一些新手问题,其中之一是这样的:当主窗体加载类的实例时,Doc创建并且构造函数打开Word文档并创建文档中所有图像的列表。在列表中的第一图像被显示在PictureBox,像这样:由兄弟控制实例化的访问对象

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
    } 

    public class Doc { 
     public List<Image> images = new List<Image>(); 
     public Doc(string path) { 
      // Open Word document, create list of images 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) { 
     Doc doc = new Doc("C:\\lorem_ipsum.doc"); 
     pictureBox1.Image = doc.images[0]; 
    } 

    private void numericUpDown1_ValueChanged(object sender, EventArgs e) { 
     pictureBox1.Image = doc.images[numericUpDown1.Value]; 
    } 
} 

还有哪些应该被用来显示不同的图像NumericUpDown控件,这就是问题所在。上例中的最后一段代码不起作用,但我希望它能说明我想要做什么。

对于这个问题(以及其中一个控件应该能够访问由其他控件创建的对象的类似问题),最佳实践解决方案是什么?我也试图通过为Doc类创建一个方法来解决它,但是却没有从那里访问图片框。

回答

2

你的问题是你创建doc作为一个局部变量。你需要一个成员变量的类的范围之内:

public partial class Form1 : Form { 
    private Doc _doc; // Add this line 

    public Form1() { 
     InitializeComponent(); 
    } 

    public class Doc { 
     public List<Image> images = new List<Image>(); 
     public Doc(string path) { 
      // Open Word document, create list of images 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) { 
     _doc = new Doc("C:\\lorem_ipsum.doc"); 
     pictureBox1.Image = _doc.images[0]; 
    } 

    private void numericUpDown1_ValueChanged(object sender, EventArgs e) { 
     pictureBox1.Image = _doc.images[numericUpDown1.Value]; 
    } 
} 

一些关于范围

public class MyClass 
{ 
    // myMemberVariable is declared inside class, but outside 
    // a function. Therefore, it can be accessed from anywhere 
    // inside the class. 
    int myMemberVariable; 

    public void MyFunction() 
    { 
     // myLocalVariable is declared inside a function. Therefore, 
     // it can be accessed only inside this function and nowhere 
     // else. 
     int myLocalVariable; 

     for (int x=0;x<10;x++) 
     { 
      // anotherLocalVariable is declared inside a for loop. Therefore, 
      // this variable can only be used inside this for loop and 
      // no where else. 
      int anotherLocalVariable; 
     } 
    } 
} 

括号作为分隔符范围的思考。您创建的变量只能在开始和结束大括号内使用,绝不可在外部使用。唯一的“部分”例外是变量static

+0

啊,那很简单。感谢大家(除了downvoter)。这种方法是否合理地接近最佳实践,或者恰恰是让我的糟糕设计发挥作用的最简单方法? :) – linurb 2015-01-20 19:57:27

+0

@linurb - 这不是“最佳做法”。这是C#中的作用域的工作原理。 – Icemanind 2015-01-20 21:04:18

+0

@linurb我想指出的是,有人可能会认为'Doc'(和'Form1')类本身的命名很差,可能不应该是Form1类中的嵌套类。如果你想/需要代码审查,你应该[检查代码审查StackExchange网站](http://codereview.stackexchange.com/)。 – Anthony 2015-01-20 23:30:55

1

只需让doc成为Form1的私人字段。

public partial class Form1 : Form { 
    private Doc doc; 

    public Form1() { 
     InitializeComponent(); 
    } 

    public class Doc { 
     public List<Image> images = new List<Image>(); 
     public Doc(string path) { 
      // Open Word document, create list of images 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) { 
     doc = new Doc("C:\\lorem_ipsum.doc"); 
     pictureBox1.Image = dok.images[0]; 
    } 

    private void numericUpDown1_ValueChanged(object sender, EventArgs e) { 
     pictureBox1.Image = doc.images[numericUpDown1.Value]; 
    } 
} 
1

doc你必须有一个局部变量,即它是本地Form1_Load。这意味着它只存在于该方法内部。你想要的是一个成员字段,在Form1类本身上定义。只要形式的存在,将坚持围绕:

public partial class Form1 : Form 
{ 
    private Doc m_Doc; 

    .... 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     m_Doc = new Doc("C:\\lorem_ipsum.doc"); 
     pictureBox1.Image = m_Doc.images[0]; 
    } 

    private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
    { 
     pictureBox1.Image = m_Doc.images[numericUpDown1.Value]; 
    } 
} 

现在m_Doc将是在类(和嵌套类为好)任何访问,但没有别的,因为它是private

我也选择添加一个m_后缀。这是没有必要的,人们会争论什么会议最好整夜,但这是我更喜欢的!