A short way to create arrays?

In VB.NET I create my arrays like

Dim myArray = New ArrayList 

But is there no way to create an array with elements without having to make a variable?

In Ruby, the long way was

 array = Array.new 

And a simple, non-variable way was just

 [element,element,...] 
0
source share
4 answers

Well, the things you can do with primitive (and String) arrays:

 Dim array As New String() Dim array As New String() { "one", "two", "three" } If (New String() { "one", "two", "three" }).Contains("one") Then ' Do something for "one" End If 

If you upgrade to VB.NET 2010, you will get additional array initialization capabilities, but if you use 2008 or below the shortest, you can create your own lists, maybe something like this:

 Dim list As New List(Of String) list.AddRange(New String() { "one", "two", "three" }) 

And touch the point of declaring things without assigning them to a variable: .NET is strongly typed, therefore, although you do not always need to declare a variable, your objects should always be of the same type (and must be specified via New ).

+2
source

I'm not sure how useful such a beast has been since then, without a name you cannot easily access its elements.

I know that C has a function that allows this using "single" access, for example:

 char hexchar = "0123456789abcdef"[nybble]; 

but after completing this statement, the char array containing this string is no longer available.

If you need an array that you can access continuously, I suspect it will need an identification name. Maybe I'm wrong, I did not use VB with VB6, but even if it is possible, it is a dubious language function (IMO).

0
source

You can do a few things.

 Public Sub Main() Dim xs = New Integer() {1, 2, 3} CType({1, 2, 3}, Integer()).CopyTo(...) Dim s2 = Sum({1, 2, 3}) End Sub Public Function Sum(ByVal array As Integer()) As Integer Return array.Sum() End Function 

That's what you need?

0
source
  For Each foo As String In New String() {"one", "two", "three"} 'an array with no name - "On the first part of the journey..." Debug.WriteLine(foo) Next 
0
source

Source: https://habr.com/ru/post/1337260/


All Articles