2015-01-20 36 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_後綴。這是沒有必要的,人們會爭論什麼會議最好整夜,但這是我更喜歡的!