2017-06-20 219 views
0

我的日常需要一個字符串數組:如何將字符串轉換爲字符串數組String()?

Private Sub AddToQueue(asFiles() As String) 
    ... 
End Sub 

我希望能夠讓一個屬性同時接受一個String和一個String()陣列。但是,由於屬性不能超載,我寫了兩個單獨/複數屬性。雖然複數變體是沒有問題的:

Public Property AddFiles As String() 
    ... 
    Set(asValue As String()) 
     AddToQueue(asValue) 
    End Set 
End Property 

但是,單個字符串變體需要轉換爲字符串數組。

Public Property AddFile As String 
    ... 
    Set(sValue As String) 
     AddToQueue(...)    'How to convert sValue to String()? 
    End Set 
End Property 

我找不到任何轉換功能,允許我這樣做,一般的錯誤是「()字符串」

值類型「字符串」不能被轉換爲。

當然,必須有辦法將這個單一字符串傳遞給我的例程?

+1

'AddToQueue(New String(){sValue})'? –

+0

'AddToQueue({sValue})'? – Pikoh

+0

@VisualVincent:所有這麼新......對不起,很瑣碎。消除問題的良好做法? – Herb

回答

1

只需使用一個array initializer,並將結果傳遞給方法:

AddToQueue(New String() {sValue}) 

以上也可以這樣寫:

AddToQueue({sValue}) 

但我更願意是明確的。 :)

相關問題