vb .net multi-dimensional arrays

Discussion in 'OT Technology' started by Slid., Dec 28, 2005.

  1. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    I am stumped with arrays in .net -- for some reason I can't define a multi-dimensional array and then add values to it. In PHP I could do this:

    Code:
    $array[0][0] = "First Name";
    $array[0][1] = "Middle Name";
    $array[0][2] = "Last Name";
    
    $array[1][0] = "First Name2";
    $array[2][1] = "Middle Name2";
    $array[3][2] = "Last Name2";
    
    etc.
    
    So in .NET I assumed that I could do this:

    Code:
    Dim st(,) As String = {{"", "", ""}}
    
    st(0, 0) = "First Name"
    st(0, 1) = "Middle Name"
    st(0, 2) = "Last Name"
    
    st(1, 0) = "First Name2"
    st(2, 1) = "Middle Name2"
    st(3, 2) = "Last Name2"
    
    If I attempt to compile the .net version that first set of values goes in properly but once I get to st(1, 0) = "First Name2" everything bombs.

    Everything that I've seen that tries to explain .net multi-dimensional arrays shows you how to define the arrays but never how to add values at runtime -- this is vital for what I am doing. I don't want a static array, I need it to be able to change.

    This is extremely frustrating as arrays have always been something so simple to use -- this isn't the first time that .net has taken something simple and turned it into a nightmare.

    Any help would be greatly appreciated.

    I have VS2003 and a beta version of VS2005.
     
  2. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Eh, for the time being I can:

    Code:
            Dim St2(500, 500) As String
    
            St2(0, 0) = "n1"
            St2(1, 0) = "n2"
            St2(2, 0) = "n3"
            St2(2, 1) = "n3-1"
            St2(2, 2) = "n3-2"
            St2(2, 3) = "n3-3"
    
            St2(3, 0) = "n4"
            St2(0, 0) = "n1"
            St2(1, 0) = "n2"
    
            MsgBox(St2(2, 5))
    
    For some reason I can't say "Dim St2(,) As String" -- it wants some type of bounds but I can ReDim at runtime to whatever size I need.
     
  3. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    What is the compiler error message?

    I agree, changing values at runtime seems like a trivial request. I don't use these trendy 'visual' .NET languages so I am not familiar with how you allocate memory. To dynamically allocate an array on the heap using C I would do something like:
    Code:
    char* array = calloc(10, sizeof(char));
    array[0] = 'h';
    array[1] = 'e';
    array[2] = 'l';
    array[3] = 'l';
    array[4] = 'o';
    array[5] = '\0';
    
    Alternatively for static allocation on the stack I would do:
    Code:
    const char* array = "hello";
    
    If you try to modify the contents of the array on the stack, the compiler will throw an error message. Perhaps that is what you are encountering? Hope that helps you somewhat.
     
  4. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,382
    Likes Received:
    580
    When you did that, you explicitly defined st.

    You did the same thing as this:
    dim st(1,3) as string
    st(0,0) = ""
    st(0,1) = ""
    st(0,2) = ""

    And unless I'm mistaken, you can't have dynamically sized arrays. You'll need to specify upper limits, like 500 or however many rows you need:
    dim st(500,3) as string
     
  5. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Any reason you're not creating an object for your data and storing each of those in an array?
     
  6. EvilSS

    EvilSS New Member

    Joined:
    Jun 11, 2003
    Messages:
    5,104
    Likes Received:
    0
    Location:
    STL
    There are two ways around this. The first is to use REDIM PRESERVE statements when you need them to resize the array. The PRESERVE keeps the array contents intact. The second (and better) way is to use an ArrayList. It is a little different but gets the same results as a dynamic array. It also doesn't take the performance hit that REDIM PRESERVE can create for large apps (especially web apps). More info here:
    http://abstractvb.com/code.asp?A=1030
     
  7. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,382
    Likes Received:
    580
    Nice. Learn something new every day. :cool:

    So arraylist is basically a more flexible linked list?
     
  8. EvilSS

    EvilSS New Member

    Joined:
    Jun 11, 2003
    Messages:
    5,104
    Likes Received:
    0
    Location:
    STL
    Pretty much. Has some nice features like built in sorting and such too.
     
  9. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Not exactly. Make sure you use the data structure that is best suited for your application. A LinkedList is a structure of nodes not aligned side by side in memory, where the ArrayList's nodes are. As a result, you can't index a LinkedList like you can an ArrayList. Basically, different structures for different purposes.
     
  10. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,382
    Likes Received:
    580
    If an ArrayList can be sized dynamically, how can it be contiguous in memory?
     
  11. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Because when you add a new element, the ArrayList class is copying the existing internal array into a new array, only 1 element greater in size. Pretty expensive as n get larger. Check out the implementation in the Java src package.
     
  12. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,382
    Likes Received:
    580
    If that's so, then :ugh: Doesn't seem very efficient. Does VB implement it the same way, or as a linked list, the way C++ implements a vector?
     
  13. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I wish we could read the C#/VB.NET implementation. A LinkedList is different in that each node added just points to the next and previous in the list. So, there is no copying of internal structures as you add and remove nodes. The only thing going on when you add an element to a LinkedList is the prev/next pointers getting set based on the new element being added.

    An ArrayList isn't a bad way to go generally. Mostly because it's simple to use, you can index it for direct access, and you normally aren't dealing with enough data to worry about the copy issue.
     
  14. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,382
    Likes Received:
    580
    Yeah I had to write my own (simplified) implementation for a class, and I kind of enjoyed it. :Ownedd:

    I guess... Having practically unlimited ram and hdd space has made us lazy as programmers, hasn't it? :hs:
     
  15. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I think it's good practice for everyone to have to create their own implementations of common data structures. I remember writing ArrayList, LinkedList, BinarySearchTree, and HashTable. Nothing all that hard but you can really get some appreciation for them by doing it yourself.

    Fo sho. :hs: That's why I'm trying to get into assembly, OS design, compilers, etc.
     
  16. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,382
    Likes Received:
    580
    :werd: I'm working on heaps now just to entertain myself until school starts back up. :)

    Yeah, I'd love to learn assembly. Have you ever read "The story of Mel, a real programmer"? :bowdown:
     
  17. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Can't say that I have. I'll search around for it. Lately, I've read Pragmatic Programmer and Just For Fun (Linus Torvalds). I just started Applied Cryptography, but I'm reading that more as a reference text compared with the above titles. I'm waiting on Assembly Step by Step right now. I wanted to get a heads up on this stuff considering a class I'm taking in the spring is about MIPS assembly. I just graduated from Information Systems a few weeks ago and I'm going back for my masters in Computer Science. I've got about 4 undergrad level courses to take in the engineering school, including this assembly class. Should be good times. :)
     

Share This Page