2014-04-03 123 views
0

我打算使用方法或函數來創建我需要的目錄樹,並將新路徑返回給調用過程。c#從函數返回值的錯誤

但我不能讓它編譯

我得到一個錯誤cannot convert method group 'localAttachmentsPath' to non delegate type string ..

這裏是我的代碼 - 我做了什麼錯?還有更好的方法來完成這個嗎?

 private string localAttachmentsPath(ref string emailAttachmentsPath) 
     { 

      string sYear = DateTime.Today.Year.ToString(); 
      string sMonth = DateTime.Now.ToString("MMMM"); 
      string sDirectoryDate = DateTime.Today.ToString("dd.MM.yyyy"); 

      if (!Directory.Exists(emailAttachmentsPath)) 
      { 
       Directory.CreateDirectory(emailAttachmentsPath); 

      } 

      emailAttachmentsPath = emailAttachmentsPath + "\\" + sYear; 
      if (!Directory.Exists(emailAttachmentsPath)) 
      { 
       Directory.CreateDirectory(emailAttachmentsPath); 
      } 

      emailAttachmentsPath = emailAttachmentsPath + "\\" + sMonth; 
      if (!Directory.Exists(emailAttachmentsPath)) 
      { 
       Directory.CreateDirectory(emailAttachmentsPath); 
      } 

      emailAttachmentsPath = emailAttachmentsPath + "\\" + sDirectoryDate; 
      if (!Directory.Exists(emailAttachmentsPath)) 
      { 
       Directory.CreateDirectory(emailAttachmentsPath); 
      } 

      //localAttachmentsPath = emailAttachmentsPath; 
      return localAttachmentsPath; 

     } 
+1

什麼是你想回來?你現在所做的只是返回一個對你自己的函數的引用,而不是一個值。如果你想返回emailAttachementsPath,只需輸入「return emailAttachmentsPath」,你不需要將函數的名字設置爲返回值(是VB語法嗎?) – DoctorMick

+0

而且你也知道,'CreateDirectory'已經創建了整個樹,如果你通過一個完整的路徑,你不需要通過子文件夾來創建它的子文件夾... –

+0

@Bartdude:謝謝,現在我不需要函數:) –

回答

0

你可以讓你的返回類型void,並刪除return聲明爲您傳遞變量emailAttachmentsPath作爲參考變量,因此wouldbe從呼叫者更新沒有返回聲明。

你的方法應該是這樣的:

private void localAttachmentsPath(ref string emailAttachmentsPath) 
{ 

//your code 

//no return statement 

} 
0

它看起來像這個問題是這樣的:

return localAttachmentsPath; 

這是一個功能,我想你想十二月帶有除函數名稱之外的其他名稱的局部變量。

0

如果使用ref string emailAttachmentsPath比你不需要返回任何字符串