Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Dyanamic array declarations

Dyanamic array declarations

Scheduled Pinned Locked Moved Visual Basic
questiondata-structureshelptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    danasegaranea
    wrote on last edited by
    #1

    HI all, I want to declare dyanaic arrays.How can I. I am trying like this but it is giving error ? Dim Names() As String Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub How to solve this Thanks in Advance Dana

    K D 2 Replies Last reply
    0
    • D danasegaranea

      HI all, I want to declare dyanaic arrays.How can I. I am trying like this but it is giving error ? Dim Names() As String Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub How to solve this Thanks in Advance Dana

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      I believe your problem is you haven't created your Names array yet. So I think your code should be something like: Dim Names() As String Sub New(ByVal st As List(Of String)) if Names is nothing then Names = new String(st.Length) end if st.CopyTo(Names) <-- value cannot be null error End Sub Anyway, hopefully you get the idea. Ben

      D 1 Reply Last reply
      0
      • K kubben

        I believe your problem is you haven't created your Names array yet. So I think your code should be something like: Dim Names() As String Sub New(ByVal st As List(Of String)) if Names is nothing then Names = new String(st.Length) end if st.CopyTo(Names) <-- value cannot be null error End Sub Anyway, hopefully you get the idea. Ben

        D Offline
        D Offline
        danasegaranea
        wrote on last edited by
        #3

        Thanks Ben, But it is giving RunTime Error.Becuse these method (Names = new String(st.Length)) is not supported in Vb.net Dana

        K 1 Reply Last reply
        0
        • D danasegaranea

          Thanks Ben, But it is giving RunTime Error.Becuse these method (Names = new String(st.Length)) is not supported in Vb.net Dana

          K Offline
          K Offline
          kubben
          wrote on last edited by
          #4

          Ok try this: private Names As String() Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub Ben

          1 Reply Last reply
          0
          • D danasegaranea

            HI all, I want to declare dyanaic arrays.How can I. I am trying like this but it is giving error ? Dim Names() As String Sub New(ByVal st As List(Of String)) st.CopyTo(Names) <-- value cannot be null error End Sub How to solve this Thanks in Advance Dana

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            It doesn't work because the Names array doesn't have any elements in it. If you delcared the array as:

            Dim Names(10) As String
            

            it would contain 11 elements of type String, all with the value of String.Empty. The way you've done it, there are no elements in the array. You'd have to ReDim the array to the proper size, then call CopyTo

            Sub New(ByVal st As List(Of String))
                ReDim Names(st.Count - 1)
                st.CopyTo(Names)
            End Sub
            

            But, I highly recommend changing the array to a List.

            Private \_names As List(Of String)
            
            Public Sub New(ByVal st As List(Of String))
                \_names.AddRange(st)
            End Sub
            

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            D 1 Reply Last reply
            0
            • D Dave Kreskowiak

              It doesn't work because the Names array doesn't have any elements in it. If you delcared the array as:

              Dim Names(10) As String
              

              it would contain 11 elements of type String, all with the value of String.Empty. The way you've done it, there are no elements in the array. You'd have to ReDim the array to the proper size, then call CopyTo

              Sub New(ByVal st As List(Of String))
                  ReDim Names(st.Count - 1)
                  st.CopyTo(Names)
              End Sub
              

              But, I highly recommend changing the array to a List.

              Private \_names As List(Of String)
              
              Public Sub New(ByVal st As List(Of String))
                  \_names.AddRange(st)
              End Sub
              

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              D Offline
              D Offline
              danasegaranea
              wrote on last edited by
              #6

              Thanks Dave..... It worked well Dana

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups