0

Currently I am using this to create a string array

New String() {" ", ".", ";"}

Is there another way that does not require so many characters, say without the New String() part perhaps?

1

2 Answers 2

3

The compiler can usually infer the type of the array from its elements, e.g.

Dim stringArray = {" ", ".", ";"}

Note - this doesn't work when the array is empty, because there are no elements to examine.

Dim stringArray = New String() {} ' specify the source type, infer the destination type
Dim stringArray As String() = {}  ' specify the destination type, infer the source type
Sign up to request clarification or add additional context in comments.

2 Comments

I had been passing in character arrays as an argument to a filter function like Filter(SomeString, New String(){".", ","}) Wish I would have just tried Filter(SomeString, {".", ","})
If you want an empty array Dim stringArray As New String() would be shorter without Infer being on.
0
Dim stringArray = " . ".Cast(Of String)().ToArray()

2 Comments

Cool ... throws an exception (invalid cast from char to string) here (VB2012)
Dim s = "ABC".Select(Function(c) c.ToString).ToArray. This one works, but you only save characters (in source) for longer arrays of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.