2016-03-23 27 views
1

VB.NET返回號碼的2D陣列 - 從函數返回的數字的2D陣列VB.NET - 從一個函數

我有產生一組x和y座標的二維數組的函數但我無法找到返回座標的方法。

這是我嘗試實現的簡化版本,但這會產生以下錯誤:類型'Double()'的值不能轉換爲'Double'。

Function myFunc(ByVal myVar As Double) As Double 

    Dim myArr(2, 1) As Double 

    myArr(0, 0) = 1 * myVar 
    myArr(1, 0) = 2 * myVar 
    myArr(2, 0) = 3 * myVar 
    myArr(0, 1) = 4 * myVar 
    myArr(1, 1) = 5 * myVar 
    myArr(2, 1) = 6 * myVar 

    Return myArr 

End Function 

我已經嘗試了許多不同的方法,但我只新VB和可能 簡單的東西。任何幫助,將不勝感激。

+0

錯誤消息告訴你什麼是錯的。你的函數被定義爲「As Double」,但你試圖返回'Double(,)'。因此,定義函數'As Double(,)' – Plutonix

+0

如果我將函數更改爲'As Double(,)',它仍會產生相同的錯誤。我相信我將myArr定義爲與函數返回定義不同,但不知道如何糾正它。 – Shaun

+0

不在代碼中顯示你沒有。它在其他地方顯示不是嗎? – Plutonix

回答

1

您需要返回Double(,),不Double。它應該是:

Function myFunc(ByVal myVar As Double) As Double(,) 

    Dim myArr(2, 1) As Double 

    myArr(0, 0) = 1 * myVar 
    myArr(1, 0) = 2 * myVar 
    myArr(2, 0) = 3 * myVar 
    myArr(0, 1) = 4 * myVar 
    myArr(1, 1) = 5 * myVar 
    myArr(2, 1) = 6 * myVar 

    Return myArr 

End Function 
+0

謝謝你,我第一次嘗試它會產生同樣的錯誤,但那是由於我調用函數的方法而不是函數本身。我的錯。 – Shaun

-1

我設法解決它通過更改函數和myArr定義從DoubleObject。函數的結果是完整的數組,但是作爲一個對象存儲。不知道這是否是最好的方法,但它的工作原理。

Function myFunc(ByVal myVar As Double) As Object 

Dim myArr(2, 1) As Object 

myArr(0, 0) = 1 * myVar 
myArr(1, 0) = 2 * myVar 
myArr(2, 0) = 3 * myVar 
myArr(0, 1) = 4 * myVar 
myArr(1, 1) = 5 * myVar 
myArr(2, 1) = 6 * myVar 

Return myArr 

End Function 
+0

這不是要走的路。你應該返回'Double(,)',而不是'Object'。 – Enigmativity