2013-07-30 47 views
0

我有一個第三方的DLL添加到函數的我的網頁application.one看起來像這樣無法理解如何調用該函數

public InterestCategory[] gvc(string st) 
{ 
    object[] results = this.Invoke("getit", new object[] { 
        st}); 
    return ((InterestCategory[])(results[0])); 
} 

,你可以看到該函數返回InterestCategory[]。當我檢查(GoToDefinition)InterestCategory我可以看到這個

public partial class InterestCategory 
{ 
    private string descriptionField; 
    public string Description 
    { 
     get 
     { 
      return this.descriptionField; 
     } 
     set 
     { 
      this.descriptionField = value; 
     } 
    } 
} 

現在在我的代碼我想這樣調用

API.InterestCategory IC = new API.InterestCategory(); 
    IC = api.gvc(st); 
這個功能

它拋出這樣的錯誤

Cannot implicitly convert type 'API.InterestCategory[]' to 'API.InterestCategory' 

任何一個可以告訴我什麼是正確的程序來調用這個函數

+2

看看返回類型'InterestCategory []'現在看看你正在設置返回值到'InterestCategory'。現在看看錯誤消息:'不能隱式地將類型'API.InterestCategory []'轉換爲'API.InterestCategory''。現在應該很明顯,你需要將返回值存儲在一個「InterestCategory []」中。 – dtsg

+1

+1因爲這個問題被拒絕了,但是作爲noob的人應該被壓低。 –

回答

2

您正在爲變量指定錯誤的類型。當函數返回一個數組InterestCategory[]時,你告訴編譯器你想創建一個類型爲InterestCategory的變量。

你的代碼改成這樣,它應該很好地工作:

API.InterestCategory[] ICs; 
ICs = api.gvc(securityToken); 
2

IC應該是Api.InterestCategory數組。相反,您聲明變量爲Api.InterestCategory。嘗試:

Api.InterestCategory[] IC = api.GetValidInterestsCategories(securityToken); 
1

API.InterestCategory IC = new API.InterestCategory();

所以打字

IC = api.gvc是錯誤的,因爲IC是InterestCategoryInterestCategory[]

嘗試:

var IC = api.gvc(securityToken) 
2

該方法返回一個數組,所以你米ust將結果賦給正確類型的變量:

InterestCategory[] ics = api.gvc(securityToken); 
0

你好,你正在做的一切權利,但問題你得到由於返回類型和變量的不匹配要在其中儲存。 。我不知道你爲什麼不能理解這個問題。它屬於編程的基礎。

api.InterestCategory[] ic = api.gvc(securityToken);