creating an array at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rekhamanoj
    New Member
    • Mar 2008
    • 3

    creating an array at runtime

    hello all
    how to create an array at runtime in vb
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by rekhamanoj
    hello all
    how to create an array at runtime in vb
    yo cannot create it at run time, you must have it already defined. But, you can define it as:

    dim MyArr()

    and then, during runtime, you can ReDim it:

    ReDim MyArr(1 to n, 1 to m)

    You can also have a Variant, and if you asign some list, range, collection, etc to it, it'll become a Variant's array.

    HTH

    Comment

    • jg007
      Contributor
      • Mar 2008
      • 283

      #3
      depends what type of array you mean , I have used the code below to create and array of labels on runtime

      [CODE=vbnet]
      Public Class Form1
      Public Mylabel(4) As System.Windows. Forms.Label

      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load


      Dim n As Integer

      ' text for diverted tasks
      Dim Div_text(4) As String
      Div_text(1) = "A"
      Div_text(2) = "B"
      Div_text(3) = "C"
      Div_text(4) = "D"

      'add labels to array

      For n = 1 To 4
      Mylabel(n) = New System.Windows. Forms.Label
      Next n

      ' set up label size and location

      For n = 1 To 4
      Mylabel(n).Text = New String(Div_text (n))
      Mylabel(n).Loca tion = New System.Drawing. Point(200, (22 * n) - 10)

      ' add label to form

      Me.Controls.Add (Mylabel(n))
      Next

      End Sub[/CODE]
      Last edited by Killer42; Mar 25 '08, 03:03 AM. Reason: Changed CODE tag to CODE=vbnet, added closing tag.

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by jg007
        depends what type of array you mean , I have used the code below to create and array of labels on runtime
        Oh, that's true, you can make 'arrays' of controls during runtime. May be they can be more helpful for what you need, rekhamanoj!

        Just have in mind that controls might be slower than variables. But for small amounts of data, it can be a nice solution.

        Comment

        • rekhamanoj
          New Member
          • Mar 2008
          • 3

          #5
          thank you so much for your timely help

          Comment

          Working...