locked
initialize list of string RRS feed

  • Question

  •  

     

    Dim pix() As new List(Of String) = {"*.jpg", "*.avi", "*.mov"}
    Dim pix As new List(Of String)() = {"*.jpg", "*.avi", "*.mov"}
    
    Dim pix() As List(Of String) = {"*.jpg", "*.avi", "*.mov"} Dim pix As List(Of String)() = {"*.jpg", "*.avi", "*.mov"}

    Does it matter where the () are?

    What difference does New make?

    Tuesday, July 6, 2010 4:50 PM

Answers

  • I would suggest using a List, it is much better than an array and much easier to work with. You don't need ANY sets of empty parens when working with it:

    Dim pix As new List(Of String) From {"*.jpg", "*.avi", "*.mov"}

    In this case, pix is the new list. The new keyword is required to define the list as a new list.

    Instead of "=", you need the "From" keyword.

    This syntax creates a new list of strings "from" the defined set of strings.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, July 6, 2010 6:01 PM
  •   dim a() as list(of string)
      dim a as list(of string)()

    are the same.  They create a variable named a which is an array of lists of strings.  So a(0) is a list of strings, a(1) is a list of strings, and so one

      dim a as list(of string)

    is creating a variable named a that is a list of strings.  a(0) is a string, a(1) is a string, and so on.

     

    Wednesday, July 7, 2010 2:26 AM

All replies

  • I don't think you can do it that way

    pix() would be an array, not a List(Of T)

    normally you would populate a List this way

        Dim pix As New List(Of String)
        pix.AddRange(New String() {"*.jpg", "*.avi", "*.mov"})
    
    Tuesday, July 6, 2010 5:00 PM
  • thx I'll try array.

    Does it matter where the () are?

    What difference does New make?

    Tuesday, July 6, 2010 5:09 PM
  • no, but I would still use a List(Of String) instead

    much easier to work with, especially having to use a messy ReDim statement with an array

    Tuesday, July 6, 2010 5:11 PM
  • Hi Bob Witte and Joe,

    You could also write in one line like this.>>

     

    Dim pix As List(Of String) = (New String() {"*.jpg", "*.avi", "*.mov"}).ToList

     

    To Bob,

    Yes it matters where the rounded brackets >> () << or parentheses are. ;-)


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, July 6, 2010 5:41 PM
  • I would suggest using a List, it is much better than an array and much easier to work with. You don't need ANY sets of empty parens when working with it:

    Dim pix As new List(Of String) From {"*.jpg", "*.avi", "*.mov"}

    In this case, pix is the new list. The new keyword is required to define the list as a new list.

    Instead of "=", you need the "From" keyword.

    This syntax creates a new list of strings "from" the defined set of strings.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, July 6, 2010 6:01 PM
  • What difference does New make?

    Hi again Bob Witte,

    Have a read of this.>>

     

    Breakdown of a declaration.

    Dim list1 As List(Of String)

     

    Dim is short for DIMension and it reserves a space in memory.

    list1 and list2 ( below ) are the variables.

    As precedes the last term and it makes the declaration appear close to English as in;

    Create aVariable As Integer

     

    New in the declaration below causes a New object to be created in the reserved area of memory.

    Think of a box in which you can place an object like a ball, you can have an empty box or a box with a New object in it. Use New with all Classes. There are occasions where Vb.Net will tell you, New is not required and give a message like;

     Object of type [whatever] does not have a default constructor.

    Often, if you have an empty array or an empty array element you can get the error;

    “Object instance not set to an instance of an object.”

    Then try adding the word New

     

    With a value type like any of the following;

    Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Short,

     Integer, Long, UShort, UInteger, ULong, Double or Decimal

    you do not need to use New

     

    Dim list2 As New List(Of String)

     


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, July 6, 2010 11:41 PM
  • Hi again Bob Witte,

    There are other keywords other than Dim that can be used to set up a variable or object.

    I will not go into more detail here, unless you ask about the keywords PRIVATE, PUBLIC, STATIC and SHARED.

    If you want to know, please start a new question.

    _________________________________________

     

    In the meantime try this code with one button on a Form and see the difference NEW makes.

    Paste ALL of this into your Form1 code window please.>>

     

    Option Strict On
    
    Public Class Form1
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim example1 As New ExampleClass(Color.Blue)
        MessageBox.Show(example1.Color.ToString)
    
        'or like this.>>
        Dim example2 As ExampleClass = New ExampleClass(Color.DarkSeaGreen)
        MessageBox.Show(example2.Color.ToString)
    
        'or like this.>>
        Dim example3 As New ExampleClass With {.Color = Color.Gold}
        MessageBox.Show(example3.Color.ToString)
    
        'or even.>>
        Dim example4 As New ExampleClass
        example4.Color = Color.Silver
        MessageBox.Show(example4.Color.ToString)
    
        'or even.>>
        Dim example5 As New ExampleClass
        With example5
          .Color = Color.CornflowerBlue
        End With
        MessageBox.Show(example5.Color.ToString)
    
        'Will throw an error as NEW not used.>>
        Dim example6 As ExampleClass
        example6.Color = Color.Purple
        MessageBox.Show(example6.Color.ToString)
    
      End Sub
    End Class
    
    Public Class ExampleClass
    
      Public Sub New()
        'Deliberately left with no code.
      End Sub
    
      Public Sub New(ByVal clr As Color)
        Me.myColor = clr
      End Sub
    
      Private myColor As Color
    
      Public Property Color() As Color
        Get
          Return Me.myColor
        End Get
        Set(ByVal value As Color)
          Me.myColor = value
        End Set
      End Property
    
    End Class


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 12:09 AM
  • thx.

    I suspect my real quesiton has more to do w () than 'new'. [see original post]

    I use lists/arrays of strings. To my thinking, one sohould dim as

    dim a() as list of string

     

    Looks to me like an list, w each element, a string

    Then there's

    dir a as list of string()

    and I wonder what that is, or how it's different.

    It looks to me like a list w each element, an array of strings. I think I"ve written it both ways and it always sem to work. This is the origin of my question "doesit matter where the () are"? Is this two ways to dim the same thing, or are they different?

    In trying variations to see if I can stumble into something that 'works', I've moved () in a definition above and seen 'New' drop off somewhere. I don't stick in 'New' unless I get a snoippit w one or IDE tells me I need it. I don't understand how I can use a variable that hasn't been instanced by the compiler. It seems like a compoiler design error to allow/require 'new' if I can dim a variable and then use it w/o 'new'.  If I can't do that in some instances and can't in others, I have no clue as to which are which.  AS I say, it seems to me that 'new' would be required in every dim and if that were the ase, it is the saem thigns as not having it in the language [if it's always there, either I suupply it or the computer knows it needs to be there and should supply it without bothering me].

    I can't imagine why I would define a variable that I would plan to never use, ie leave off 'new' only when I intend it that way - uninstanced.

     

    In the 1st post,

    Dim pix() As new List(Of String) = {"*.jpg", "*.avi", "*.mov"}
    Dim pix As new List(Of String)() = {"*.jpg", "*.avi", "*.mov"}


    Dim pix() As List(Of String) = {"*.jpg", "*.avi", "*.mov"}
    Dim pix As List(Of String)() = {"*.jpg", "*.avi", "*.mov"}

    "What difference does New make?" means "is the use/non-use of 'New' part of my problem?

    Ditto "Does it matter where the () are?" means is teh location of () part of why I have trouble initializing the list?

    From DeborahK above, I see teh correct way to do this for lists, but I still have these questions about () & New.

    Wednesday, July 7, 2010 12:28 AM
  • I would suggest using a List, it is much better than an array and much easier to work with. You don't need ANY sets of empty parens when working with it:

    Dim pix As new List(Of String) From {"*.jpg", "*.avi", "*.mov"}

    In this case, pix is the new list. The new keyword is required to define the list as a new list.

    Instead of "=", you need the "From" keyword.

    This syntax creates a new list of strings "from" the defined set of strings.


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

    Hi ALL,

    Please note: The above suggestion from DeborahK is for 2010 versions of VB.Net

     


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 12:37 AM
  • thx.

     but I still have these questions about () & New.

    Hi Bob Witte,

    My tip: Do not use any () where you don't need them. :-)    :-D

     

    The rounded brackets or parentheses here.>>

     

    Dim anArray() As String

    or.>>

    Dim anArray As String()

    in fact creates an empty array of type STRING

    You can not put them in both positions like this.>>

    Dim anArray() As String()

     

     

    Public Class Form1
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
     
    
     Dim oneDarray() As Integer
     'or
     Dim oneDimensionalArray As Integer()
    
     Dim twoDarray(,) As Integer
     'or
     Dim twoDimensionalArray As Integer(,)
    
     Dim threeDArray(,,) As Integer
     Dim threeDimensionalArray As Integer(,,)
    
     'Please note the number of commas is one less than the number of array dimensions.
    
     'You can pass an entire array to a SUB or FUNCTION as shown below.>>
     Example1(oneDarray)
     Example2(oneDimensionalArray)
    
     Example3(twoDarray)
     Example4(twoDimensionalArray)
    
     Example5(threeDArray)
     Example6(threeDimensionalArray)
    
     twoDarray = ExampleFunction1(oneDarray)
    
     End Sub
    
     Public Sub Example1(ByVal aOneDArray() As Integer)
    
     End Sub
    
     'or
     Public Sub Example2(ByVal aOneDArray As Integer())
    
     End Sub
    
     Public Sub Example3(ByVal aTwoDArray(,) As Integer)
    
     End Sub
    
     Public Sub Example4(ByVal aTwoDArray As Integer(,))
    
     End Sub
    
     Public Sub Example5(ByVal aThreeDArray(,,) As Integer)
    
     End Sub
    
     Public Sub Example6(ByVal aThreeDArray As Integer(,,))
    
     End Sub
    
     ''' <summary>
     ''' Function expects a one dimensional array of type Integer,
     ''' it returns a two dimensional array of type Integer.
     ''' </summary>
     ''' <param name="aOneDArray"></param>
     ''' <returns>Returns a two dimensional array of type Integer</returns>
     ''' <remarks></remarks>
     Public Function ExampleFunction1(ByVal aOneDArray As Integer()) As Integer(,)
    
    
     End Function
    
    End Class
    

     


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 1:01 AM
  •  

     

     

    Dim pix() As new List(Of String) = {"*.jpg", "*.avi", "*.mov"}
    Dim pix As new List(Of String)() = {"*.jpg", "*.avi", "*.mov"}
    
    Dim pix() As List(Of String) = {"*.jpg", "*.avi", "*.mov"} Dim pix As List(Of String)() = {"*.jpg", "*.avi", "*.mov"}

     

    Does it matter where the () are?

    What difference does New make?

    Hi Bob Witte,

    None of the above are valid even with different variable names.

    So the first four here are invalid.>>


     Dim pix1() As new List(Of String) = {"*.jpg", "*.avi", "*.mov"}
    
    Dim pix2 As new List(Of String)() = {"*.jpg", "*.avi", "*.mov"}
    
     Dim pix3() As List(Of String) = {"*.jpg", "*.avi", "*.mov"}
     Dim pix4 As List(Of String)() = {"*.jpg", "*.avi", "*.mov"}

     

        'This would be a one dimensional array of Lists.
        Dim pix1() As List(Of String)
    
        'So would this.>>
        Dim pix2 As List(Of String)()
    
        'But this would be a List(Of String)
        Dim pix3 As New List(Of String)()
        'Same as.>>
        Dim pix4 As New List(Of String)

     

    Like I said, Avoid using () unless you intend something to be used as an ARRAY.

     


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 1:18 AM
  • Hi again Bob Witte,

    In case you can not picture or imagine an array of LISTs see these declarations.>>

      Dim list1 As List(Of String) = (New String() {"a", "b", "c"}).ToList
      Dim list2 As List(Of String) = (New String() {"d", "e", "f"}).ToList
      Dim list3 As List(Of String) = (New String() {"g", "h", "i"}).ToList
      Dim list4 As List(Of String) = (New String() {"j", "k", "l"}).ToList
    
      'This would be a one dimensional array of Lists.
      Dim pix1() As List(Of String) = {list1, list2, list3, list4}
    
      '_______________________________
    
      'So would this.>>
      Dim pix2(3) As List(Of String)
    
      pix2(0) = list1
      pix2(1) = list2
      pix2(2) = list3
      pix2(3) = list4

     

    You could of course have;

    an array of arrays,

    an array of Lists

    a List of arrays and even

    a List of Lists

    but showing those may only get you a bit muddled?

    Wednesday, July 7, 2010 1:46 AM
  • Then there's

    dir a as list of string()

    and I wonder what that is, or how it's different.

    It looks to me like a list w each element, an array of strings.


    A list with each element as an array of strings would look like.>>

        Dim stringArray1 As String() = New String() {"a", "b", "c"}
        Dim stringArray2 As String() = New String() {"Hi", "there", "Bob"}
        Dim stringArray3 As String() = New String() {"with", "warm", "regards"}
        Dim stringArray4 As String() = New String() {"from", "John", "Anthony", "Oliver"}
    
        Dim myList As New List(Of String() )
    
        myList.Add(stringArray1)
        myList.Add(stringArray2)
        myList.Add(stringArray3)
        myList.Add(stringArray4)


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 2:15 AM
  •   dim a() as list(of string)
      dim a as list(of string)()

    are the same.  They create a variable named a which is an array of lists of strings.  So a(0) is a list of strings, a(1) is a list of strings, and so one

      dim a as list(of string)

    is creating a variable named a that is a list of strings.  a(0) is a string, a(1) is a string, and so on.

     

    Wednesday, July 7, 2010 2:26 AM
  •   dim a as list(of string)

    is creating a variable named a that is a list of strings.  a(0) is a string, a(1) is a string, and so on.

     

    Hi Bob Witte,

    In support of the above post from Acamar try this code snippet.>>

    Public Class Form1
    
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
     Dim a As List(Of String) = (New String() {"Hi", "there", "Bob"}).ToList
    
     MessageBox.Show(a(0) )
     'or
     MessageBox.Show(a.Item(0))
    
     End Sub
    
    End Class
    

    Tip: If you hover your mouse over your variable names Vb.Net will often show you what they are.  :-)

     


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 2:40 AM
  • OK, I'm going w "it doesn't matter which position you choose for the () [except not BOTH]".

    I'm also observing that another [prior to VB 2010?] method exists for initializing lists, namely create an initialized array and then dump it to the list.

    Since I have 2010, I'm going to stick w 'From'

     

    Thank you all for your help.

    Bob

    Wednesday, July 7, 2010 3:36 AM
  • OK, I'm going w "it doesn't matter which position you choose for the () [except not BOTH]".

    I'm also observing that another [prior to VB 2010?] method exists for initializing lists, namely create an initialized array and then dump it to the list.

    Since I have 2010, I'm going to stick w 'From'

     

    Thank you all for your help.

    Bob

    Hi Bob,

    Then mark the answer from DeborahK too please.


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 7, 2010 3:43 AM
  • thanks for reminding me
    Wednesday, July 7, 2010 4:30 AM