VB.Net Initialising an array on the fly

前端 未结 5 1650
悲哀的现实
悲哀的现实 2020-12-20 18:30

I wrote this - very simple - function, and then wondered does VB have some pre-built functionality to do this, but couldn\'t find anything specific.

Private          


        
相关标签:
5条回答
  • 2020-12-20 18:55

    Any reason not to do:

    Dim someNames() as string = New String(){"Han", "Luke", "Leia"}
    

    The only difference is type inference, as far as I can tell.

    I've just checked, and VB 9 has implicitly typed arrays too:

    Dim someNames() as string = { "Han", "Luke", "Leia" }
    

    (This wouldn't work in VB 8 as far as I know, but the explicit version would. The implicit version is necessary for anonymous types, which are also new to VB 9.)

    0 讨论(0)
  • 2020-12-20 19:02

    Microsoft recommends the following format

    Dim mixedTypes As Object() = New Object() {item1, item2, itemn}
    

    per http://msdn.microsoft.com/en-US/library/8k8021te(v=VS.80).aspx

    Note, you don't have to specify the size of new Array, as that is inferred from the initialized count of args. If you do want to specify the length, you specify not the "length" but index number of last space in array. ie. New Object(2) {0, 1, 2} ' note 3 args.

    0 讨论(0)
  • 2020-12-20 19:04
    PrintNames(New String(){"Hans", "Luke", "Lia"})
    
    0 讨论(0)
  • 2020-12-20 19:13
    Dim somenames() As String = {"hello", "world"}
    
    0 讨论(0)
  • 2020-12-20 19:17

    The following codes will work in VB 10:

    Dim someNames = {"Hans", "Luke", "Lia"} 
    

    http://msdn.microsoft.com/en-us/library/ee336123.aspx

    0 讨论(0)
提交回复
热议问题