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. create array on runtime

create array on runtime

Scheduled Pinned Locked Moved Visual Basic
csharpdata-structuresquestion
13 Posts 7 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.
  • K keninfo

    VB.NET For my application i need to create arrays on runtime. How will i do that? Or do i have to make something like a method which creates an array while developing and make an instance of it at runtime to rename it finally? Thanks -- modified at 17:24 Friday 3rd November, 2006

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Just create a method that creates your Array and call that method at runtime! Or, just put the code that creates the array in the "Form.Load" procedure. Or do you want to know how to create an array?


    Extreme Exe

    1 Reply Last reply
    0
    • K keninfo

      VB.NET For my application i need to create arrays on runtime. How will i do that? Or do i have to make something like a method which creates an array while developing and make an instance of it at runtime to rename it finally? Thanks -- modified at 17:24 Friday 3rd November, 2006

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

      Like Christian, I can't figure out what you really want to do. You're post sounds a bit confused. Are you looking for something like this?

      Dim myArray() As String
      

      creates an array of Strings. You might want to explain what your trying to do in more detail and include a code snippet showing where you want this array to show up.

      Dave Kreskowiak Microsoft MVP - Visual Basic

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Like Christian, I can't figure out what you really want to do. You're post sounds a bit confused. Are you looking for something like this?

        Dim myArray() As String
        

        creates an array of Strings. You might want to explain what your trying to do in more detail and include a code snippet showing where you want this array to show up.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        K Offline
        K Offline
        keninfo
        wrote on last edited by
        #5

        Ok, maybee a wrong start of this discussion. Let's try it again. I am writting an application (kind of callcenter server), which controls the traffic from a telephone system (PABX). The application let me insert callcenter agents(name and number), extentions(name and number) and callcenter groups(name and number). When i insert a callcenter group an array has to be created with the callcenter group number as it's name. finally i can add agents to the group array. So, for example: Two agenst were inserted: 200 Bill 201 Albert Then two extentions were made: 100 extention 100 101 extention 101 Finally one group is made: 400 Sales After creating the group, an array has to be created with 400 as it's name. The agents have to be added to the group array. So, i know how to make an array: Dim groupArray() as Integer. And i know how to add agents to the array. But during runtime there have to be an array created everytime when a group is made and the array name has to be the number of the groep. Thanks regards

        C D 2 Replies Last reply
        0
        • K keninfo

          Ok, maybee a wrong start of this discussion. Let's try it again. I am writting an application (kind of callcenter server), which controls the traffic from a telephone system (PABX). The application let me insert callcenter agents(name and number), extentions(name and number) and callcenter groups(name and number). When i insert a callcenter group an array has to be created with the callcenter group number as it's name. finally i can add agents to the group array. So, for example: Two agenst were inserted: 200 Bill 201 Albert Then two extentions were made: 100 extention 100 101 extention 101 Finally one group is made: 400 Sales After creating the group, an array has to be created with 400 as it's name. The agents have to be added to the group array. So, i know how to make an array: Dim groupArray() as Integer. And i know how to add agents to the array. But during runtime there have to be an array created everytime when a group is made and the array name has to be the number of the groep. Thanks regards

          C Offline
          C Offline
          ChandraRam
          wrote on last edited by
          #6

          I think what you probably need is a dynamic array of collections, so you can add a collection every time a new group is created. Hope this helps Chandra

          K 1 Reply Last reply
          0
          • K keninfo

            VB.NET For my application i need to create arrays on runtime. How will i do that? Or do i have to make something like a method which creates an array while developing and make an instance of it at runtime to rename it finally? Thanks -- modified at 17:24 Friday 3rd November, 2006

            O Offline
            O Offline
            OldWarhorse
            wrote on last edited by
            #7

            Google this: "jagged arrays vb .net"

            1 Reply Last reply
            0
            • K keninfo

              Ok, maybee a wrong start of this discussion. Let's try it again. I am writting an application (kind of callcenter server), which controls the traffic from a telephone system (PABX). The application let me insert callcenter agents(name and number), extentions(name and number) and callcenter groups(name and number). When i insert a callcenter group an array has to be created with the callcenter group number as it's name. finally i can add agents to the group array. So, for example: Two agenst were inserted: 200 Bill 201 Albert Then two extentions were made: 100 extention 100 101 extention 101 Finally one group is made: 400 Sales After creating the group, an array has to be created with 400 as it's name. The agents have to be added to the group array. So, i know how to make an array: Dim groupArray() as Integer. And i know how to add agents to the array. But during runtime there have to be an array created everytime when a group is made and the array name has to be the number of the groep. Thanks regards

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

              It sounds like you looking more for a database than a collection of arrays. You've just described the layout of three different tables and their relationships.

              Dave Kreskowiak Microsoft MVP - Visual Basic

              K 1 Reply Last reply
              0
              • C ChandraRam

                I think what you probably need is a dynamic array of collections, so you can add a collection every time a new group is created. Hope this helps Chandra

                K Offline
                K Offline
                keninfo
                wrote on last edited by
                #9

                Ok, But how will i do that?

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  It sounds like you looking more for a database than a collection of arrays. You've just described the layout of three different tables and their relationships.

                  Dave Kreskowiak Microsoft MVP - Visual Basic

                  K Offline
                  K Offline
                  keninfo
                  wrote on last edited by
                  #10

                  Yes, i can imagine. But i want to solve this with arrays.

                  1 Reply Last reply
                  0
                  • K keninfo

                    VB.NET For my application i need to create arrays on runtime. How will i do that? Or do i have to make something like a method which creates an array while developing and make an instance of it at runtime to rename it finally? Thanks -- modified at 17:24 Friday 3rd November, 2006

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    So you say that you need to create variables dynamically? Well, that's not possible in VB.NET. Why? Because noone ever needs to do that. Ever. You say that you need to create an array that has a specific name. Strictly speaking that is not possible either, as an array doesn't even have a name. When you create an array object you get a reference to the object. That reference is usually assigned to a reference variable. For example: Dim a As String[] = New String[42]; Here the statement New String[42] creates the actual array, and the value of that statement is the reference to the array. The variable a can hold a reference to an object of the type String[]. The reference to the array is stored in the variable. You can have any number of references to a specific object. Example: Dim b As String[] = a; Dim c As String[] = a; Now you have three references to the array, but there is still only one array. You have three references with different names, and the array doesn't have a name at all.

                    --- b { font-weight: normal; }

                    1 Reply Last reply
                    0
                    • K keninfo

                      VB.NET For my application i need to create arrays on runtime. How will i do that? Or do i have to make something like a method which creates an array while developing and make an instance of it at runtime to rename it finally? Thanks -- modified at 17:24 Friday 3rd November, 2006

                      O Offline
                      O Offline
                      OldWarhorse
                      wrote on last edited by
                      #12

                      OK. Here's what you need to do. Or what I would do.* I would use a HashTable of ArrayLists. Quick, easy, efficient, fairly robust, good cost/benefit ratio. Downside: Not strongly-typed. You don't want to use raw arrays because you would either have to oversize them to begin with (how many? 1000? 5000? Wasteful and error prone), OR you would have to keep ReDim'ing them (bad performance). You could use ArrayList to get around that problem, but you still have no good keyed access to your entries. That's where the HashTable comes in. The HashTable (or Dictionary<..>, depending on your version and requirements) simply stores entries of Key and Value pairs. In this case your Key would be the Call Center number, and it's Value would be an ArrayList. The ArrayList would store the Agent objects. Every time you hit a new call center, you create a new hashtable entry using the call center number as the key, and a new arraylist with the Agent on that call as it's initial value, which effectively allows you to "create a new variable at runtime with a particular name". (Or, instead of storing the agent object in the arraylist slot, you could create a hashtable of Agents, and just store a reference to that agent's entry in the arraylist slot.) How do you know it's a new call center? By using the ContainsKey function of the hashtable, or by simply trying to access the item with that key. If it doesn't exist, you get back Null. If it does exist, you add the agent (or a reference to the agent) to its arraylist. Simple. If you're not familiar with hashtables, take an hour or so and read up on them. Set one up and play with it. Don't be afraid of them. Very powerful tool. *Now. All that having been said, this is NOT the only way or best way to engineer this, it's just a quick-and-dirty as far as I look at it. Keep in mind what Dave K. said. What you're really doing here is modeling and managing entity relationships. Call Centers, Agents, Calls, Extensions... Which means you might be better served to use a strongly-typed DataSet, with Tables and DataRelations to begin with. You are probably going to need to do that eventually anyway. Hope this helps. Good luck.

                      K 1 Reply Last reply
                      0
                      • O OldWarhorse

                        OK. Here's what you need to do. Or what I would do.* I would use a HashTable of ArrayLists. Quick, easy, efficient, fairly robust, good cost/benefit ratio. Downside: Not strongly-typed. You don't want to use raw arrays because you would either have to oversize them to begin with (how many? 1000? 5000? Wasteful and error prone), OR you would have to keep ReDim'ing them (bad performance). You could use ArrayList to get around that problem, but you still have no good keyed access to your entries. That's where the HashTable comes in. The HashTable (or Dictionary<..>, depending on your version and requirements) simply stores entries of Key and Value pairs. In this case your Key would be the Call Center number, and it's Value would be an ArrayList. The ArrayList would store the Agent objects. Every time you hit a new call center, you create a new hashtable entry using the call center number as the key, and a new arraylist with the Agent on that call as it's initial value, which effectively allows you to "create a new variable at runtime with a particular name". (Or, instead of storing the agent object in the arraylist slot, you could create a hashtable of Agents, and just store a reference to that agent's entry in the arraylist slot.) How do you know it's a new call center? By using the ContainsKey function of the hashtable, or by simply trying to access the item with that key. If it doesn't exist, you get back Null. If it does exist, you add the agent (or a reference to the agent) to its arraylist. Simple. If you're not familiar with hashtables, take an hour or so and read up on them. Set one up and play with it. Don't be afraid of them. Very powerful tool. *Now. All that having been said, this is NOT the only way or best way to engineer this, it's just a quick-and-dirty as far as I look at it. Keep in mind what Dave K. said. What you're really doing here is modeling and managing entity relationships. Call Centers, Agents, Calls, Extensions... Which means you might be better served to use a strongly-typed DataSet, with Tables and DataRelations to begin with. You are probably going to need to do that eventually anyway. Hope this helps. Good luck.

                        K Offline
                        K Offline
                        keninfo
                        wrote on last edited by
                        #13

                        Hello OldWarhorse, Thanks a lot for your tips. I will try the hashtable. Regards

                        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