|
-
Feb 8th, 2007, 08:26 AM
#1
Thread Starter
Lively Member
[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.
-
Feb 8th, 2007, 08:32 AM
#2
Re: [2005] Array Question
When you decate an array in vb like this:
VB Code:
Dim arintPKs() as Integer
This declares an array the can be re dimmensioned at a later point in the process like this:
VB Code:
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
-
Feb 8th, 2007, 08:34 AM
#3
Re: [2005] Array Question
It's virtually the same. Declare the array variable:
VB Code:
Private integerArray As Integer()
then create the array when it's needed:
VB Code:
Me.integerArray = New Integer(x - 1) {}
You can also use ReDim:
VB Code:
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".
-
Feb 8th, 2007, 08:58 AM
#4
Thread Starter
Lively Member
Re: [2005] Array Question
Upper bound? What's the differance, they bother refer to size right?
-
Feb 8th, 2007, 09:14 AM
#5
Fanatic Member
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:
Dim IntegerArray(9) as Integer
So you have an array with index from 0 - 9 i.e. 10 elements
-
Feb 8th, 2007, 09:29 AM
#6
Thread Starter
Lively Member
Re: [2005] Array Question
Okay I understand. So I've been creating arrays with one extra element all this time in VB.
-
Feb 8th, 2007, 04:51 PM
#7
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.
-
Feb 8th, 2007, 08:33 PM
#8
Thread Starter
Lively Member
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?
-
Feb 8th, 2007, 09:15 PM
#9
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:
Dim myArray(2, 3) As Star
would look like this: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:
Dim myArray(,) As Object
ReDim myArray(2, 2)
To create a 3x3 jagged array you can NOT do this:
VB Code:
Dim myArray()() As Object
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:
Dim myArray()() As Object
ReDim myArray(2)
For Each element As Object() In myArray
ReDim element(2)
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.
-
Feb 8th, 2007, 09:17 PM
#10
Thread Starter
Lively Member
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.
-
Feb 8th, 2007, 09:19 PM
#11
Thread Starter
Lively Member
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
-
Feb 8th, 2007, 09:21 PM
#12
Thread Starter
Lively Member
Re: [2005] Array Question (update, ReDim with jagged Arrays)
Okay! I know! brb, I'll check it out
-
Feb 8th, 2007, 09:21 PM
#13
Re: [2005] Array Question (update, ReDim with jagged Arrays)
Note that you cal also do this with a multidimensional array:
VB Code:
Dim myArray(2, 2) As Object
While you cannot do this with a jagged array:
VB Code:
Dim myArray(2)(2) As Object
You would have to do something like this:
VB Code:
Dim myArray(2)() As Object
For Each arr As Object() In myArray
arr = New Object(2) {}
Next arr
The advantage of the jagged array is that you can also do something tlike this:
VB Code:
Dim myArray(9)() As Object
For i As Integer = 0 To 9
ReDim myArray(i)(i)
'This is equaivalent to:
'myArray(i) = New Object(i) {}
Next i
which would produce something like this:
Code:
**********
*********
********
*******
******
*****
****
***
**
*
You cannot do that with a multidimensional array.
-
Feb 8th, 2007, 09:23 PM
#14
Thread Starter
Lively Member
Re: [2005] Array Question (update, ReDim with jagged Arrays)
I think the (,) will work. Gah! VB is so subtle in it differances with Java
-
Feb 8th, 2007, 09:33 PM
#15
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|