iT邦幫忙

0

C# 程式進入之後使用非靜態類別

c#
tryit 2020-05-12 18:21:5212953 瀏覽
  • 分享至 

  • twitterImage

我寫了一個類似這樣的東西
程式進入點
program.cs

namespace 8787
{
    class Program
    {
        private readDocument readDocument;
        public readDocument getReadDocument()
        {
            return this.readDocument;
        }
        static void Main(string[] args)
        {
            List<string> strList = new 
            readDocument.anadocument(strList)
        }
    }
}

ReadDocument.cs

namespace 8787
{
    class readDocument
    {
        public void anadocument(List<string> strList)
        {
            #某執行程式
        }
    }
}

然後會一直跳出這個錯誤

需要有物件參考,才可使用非靜態欄位、方法或屬性 'Program.readDocument'。

請問該如何解決呢?
補充:由於anadocument還有其他用途,步行將它改成靜態類別


痾,謝謝各位幫助,我後來發現到我耍蠢了,這樣改就OK了

program.cs

namespace 8787
{
    class Program
    {
        private readDocument readDocument;
        public readDocument getReadDocument()
        {
            return this.readDocument;
        }
        static void Main(string[] args)
        {
            List<string> strList = new List<string>;
            readDocument r = new readDocument();
            r.anadocument(strList);
        }
    }
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
fillanofeng
iT邦新手 5 級 ‧ 2020-05-13 10:06:59
最佳解答
class ReadDocument {
    public void AnaDocument(List<string> strList) {
        ...
    }
}
...
class Program {
    private ReadDocument readDocument = new ReadDocument();
    public readDocument.GetReadDocument() {
        return this.readDocument;
    }
    static void Main(string[] args) {
        List<string> strList = new List<string>();
        ...
        Program app = new Program();
        app.GetReadDocument().AnaDocument(strList);
    }
}

這樣?

1
米歐
iT邦新手 3 級 ‧ 2020-05-12 18:25:35

把你的物件變成靜態類別。

static class readDocument {
    public static void anadocument(List<string> strList) {

    }
}
看更多先前的回應...收起先前的回應...
tryit iT邦研究生 4 級 ‧ 2020-05-12 18:31:21 檢舉

謝謝您的回答,但由於我的anadocument還有其他用途,不可以隨意更改他的類別

米歐 iT邦新手 3 級 ‧ 2020-05-12 18:38:26 檢舉
class readDocument {
  public List<string> anadocument(List<string> strList) {
    return null;
  }
}

class Program {
  private readDocument readDocument;
  public readDocument getReadDocument() {
    return this.readDocument;
  }
  static void Main(string[] args) {
    List<string> strList = new readDocument().anadocument(null);
  }
}

這樣呢?

tryit iT邦研究生 4 級 ‧ 2020-05-12 23:43:08 檢舉

我試試看

tryit iT邦研究生 4 級 ‧ 2020-05-13 09:58:45 檢舉

不行,而且這樣寫並不是我想要的執行結果,謝謝您的指點

2
YoChen
iT邦研究生 1 級 ‧ 2020-05-13 10:16:43

我不太曉得您要的結果是什麼~XDDD
不過要在靜態的Main當中使用readDocument欄位的話,
readDocument欄位本身就必須是靜態的才行~

題外話:類別方法命名上建議還是使用Pascal命名法,不然很容易跟變數搞混,範例中我一並幫您改掉了~XDDD

class Program
{
    // 加上static修飾字
    static private ReadDocument _readDocument;
    
    // 類別內部就不用另外寫get了
    
    static void Main(string[] args)
    {
        List<string> strList = new List<string>();
        _readDocument.AnaDocument(strList);
    }
}
class ReadDocument
{
    public void AnaDocument(List<string> strList)
    {
        // do something
    }
}

我要發表回答

立即登入回答