Results 1 to 15 of 15

Thread: [resoved][2005] Array Question (update, ReDim with jagged Arrays)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    [resoved][2005] Array Question (update, ReDim with jagged Arrays)

    I'm coming from java and in java we can declare an array like: int[] integerArray; and then later delcare how many elements the array will have with intergerArray = new Int[x];

    How can I do this in VB? I want it to have class wide scope but I wont set it's size until the class is instanated.
    Last edited by vagabon; Feb 8th, 2007 at 09:33 PM.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Array Question

    When you decate an array in vb like this:

    VB Code:
    1. Dim arintPKs() as Integer

    This declares an array the can be re dimmensioned at a later point in the process like this:

    VB Code:
    1. ReDim arintPKs(some numeric value)

    If you have data already in the array and want to keep that data use the Redim Preserve command when you reset the size.

    When done with the array I normally call Erase arrayName
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Array Question

    It's virtually the same. Declare the array variable:
    VB Code:
    1. Private integerArray As Integer()
    then create the array when it's needed:
    VB Code:
    1. Me.integerArray = New Integer(x - 1) {}
    You can also use ReDim:
    VB Code:
    1. ReDim Me.IntegerArray(x - 1)
    but I prefer not. Note that when creating arrays in VB.NET you specify the upper bound, not the length, hence the "x - 1".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question

    Upper bound? What's the differance, they bother refer to size right?

  5. #5
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [2005] Array Question

    In VB, the starting index is 0. So if you want an array of length 10 you define it as:
    VB Code:
    1. Dim IntegerArray(9) as Integer

    So you have an array with index from 0 - 9 i.e. 10 elements
    Microsoft Techie

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question

    Okay I understand. So I've been creating arrays with one extra element all this time in VB.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [resolved][2005] Array Question

    It's not so much that zrrays are zero-based that is the issue. Arrays are zero-based in all c-based languages too, including Java and C#. In C-based languages you specify the length, i.e. the number of elements, of the array when you create it. In VB.NET you specify the upper bound, i.e. the greatest index, of the array.

    I could be wrong about this but I believe that VB6 arrays were 1-based, so by specifying the greatest index in VB.NET you don't break upgraded VB6 code. I think that that's why that decision was made. Like I said, I could be wrong though.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question

    Okay, a probelm with the redim: I do private bills as String()() at the class scope and then on the forms constructor I do ReDim Me.bills(cus.length -1)(5), cus is the array of the customers that is passed to the constructor.
    Later when the program runs the ReDim it throws the same error an arraylist throws if you forget the new keyword. Object referance not set to an instance of an object
    Also, using Me.bills = new String(cus.length -1)(5) throws a compile error because it is trying to make it a 1D single item, not an array.

    Any ideas on how to fix this?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    You are mixing up a multidimensional array and a jagged array. A two-dimensional array is a matrix: a single object with a length and width where every element is a peer. If you were to draw a 2D array it would be a rectangle. For instance, this array:
    VB Code:
    1. Dim myArray(2, 3) As Star
    would look like this:
    Code:
    ***
    ***
    ***
    ***
    Note the notation in the declatation: one set of parentheses with a comma between the upper bound for each dimension. That is NOT what you are doing.

    You are creating a jagged array: a single-dimensional array where each element is a single dimensional array. It's called a jagged array because "column" may not have the same number of "rows" in it, so it's not necessarily a rectangle shape. You can NOT create a jagged array with ReDim like that because all you can do is created the outer array. Each element is an array itself so you'd have to use ReDim on each inner array too. To create a 3x3 multidimensional array you can do this:
    VB Code:
    1. Dim myArray(,) As Object
    2.  
    3. ReDim myArray(2, 2)
    To create a 3x3 jagged array you can NOT do this:
    VB Code:
    1. Dim myArray()() As Object
    2.  
    3. ReDim myArray(2)(2)
    because what that's actually saying is ReDim the element at index 2 of myArray and make it 3 elements in length. The myArray variable doesn't refer to an object so it has no element at index 2, hence your NullReferenceException. To create a 3x3 jagged array you'd need to do this:
    VB Code:
    1. Dim myArray()() As Object
    2.  
    3. ReDim myArray(2)
    4.  
    5. For Each element As Object() In myArray
    6.     ReDim element(2)
    7. Next element
    Now, having said all that, you need to decide which is appropriate for your situation: a multidimensional array or a jagged array. Once you decide then declare your array in the appropriate manner and then treat it that way all the time. Remember, a 2D array is a single array object containing all the elements while a jagged array is a 1D array containing multiple 1D arrays which then contain the elements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    A matrix is better sense the dims sould be (cus.length -1)(5), which would keep the second array the same size. But how do I do that. It wont let me. I can post code.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    Code:
    Public Class Billing
        Inherits System.Windows.Forms.Form
        Private cus As Customer()
        Private bills()() As String
    #Region " Windows Form Designer generated code "
    
        Public Sub New(ByVal costomers As Customer())
            MyBase.New()
            'deep copy of costomers
            ReDim cus(costomers.Length - 1)
            For i As Integer = 0 To costomers.Length - 1
                cus(i) = costomers.GetValue(i)
            Next
            'end deep copy, if there is a better way (I hope) the compiler
            'should optimise if it is in the standard API
            ReDim bills(cus.Length - 1)(5) 
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    Okay! I know! brb, I'll check it out

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    Note that you cal also do this with a multidimensional array:
    VB Code:
    1. Dim myArray(2, 2) As Object
    While you cannot do this with a jagged array:
    VB Code:
    1. Dim myArray(2)(2) As Object
    You would have to do something like this:
    VB Code:
    1. Dim myArray(2)() As Object
    2.  
    3. For Each arr As Object() In myArray
    4.     arr = New Object(2) {}
    5. Next arr
    The advantage of the jagged array is that you can also do something tlike this:
    VB Code:
    1. Dim myArray(9)() As Object
    2.  
    3. For i As Integer = 0 To 9
    4.     ReDim myArray(i)(i)
    5.     'This is equaivalent to:
    6.     'myArray(i) = New Object(i) {}
    7. Next i
    which would produce something like this:
    Code:
    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
    You cannot do that with a multidimensional array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    I think the (,) will work. Gah! VB is so subtle in it differances with Java

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: [2005] Array Question (update, ReDim with jagged Arrays)

    okay that works, thank you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width