954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Workaround to (true)dynamic arrays

As many of you that have worked with VB.NET know, and have been irritated by, you can't use a nonconstant variable to size an array.

A simple workaround, reply if this is a problem, is to take input on the size of the array (Yes, I know you can't do this, but wait) Now you create a constant and set it equal to the new input. While this is not the best case scenario for working with many arrays (just go multidimensional).

I hope this helps, and if it is wrong or will cause problems someone tell me but it's worked for me.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

If you mean redimming it on the fly then yes, I do that too.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
If you mean redimming it on the fly then yes, I do that too.

Well redimming does not allow you to take input for the size of an array and it serves almost no purpose on top of destroying the contents of the array unless you preserve it. Redimming just like dimming does not let you use a non-constant variable for the new size

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Well obviously you would preserve it. I don't get what you mean.

Explain with a piece of code.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Ok I think I understand what you mean now. I still haven't encountered any problems using redim preserve and array.length but apparently there is some performance issue as well. So I guess you inadvertently pointed that out. Cheers - I would have been non-the-wiser. :)

http://www.aspheute.com/english/20001025.asp

While your method might work, I think the arrayList is a better option.

http://weblogs.asp.net/eporter/archive/2003/08/07/22943.aspx

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Well what I mean is this:
If you have a situation in which you don't know the size an array needs to be, dimming and redimming just wont work, you can't define an array size with a variable.

Example

Dim x as Integer
Dim ExampleInput as Integer
ExampleInput = Val(InputBox("How Many Names do you want to put in?", "Input", 0)
Dim ExampleArray(ExampleInput) as String
For x=0 to (ExampleArray.Length-1)
     ExampleArray(x)  =  InputBox("What is the name?",  "Names",  "John Smith")
Next



It wouldn't even let you build that because you cannot use a non-constant to define the size of an array. But this would work:

Dim x as Integer
Dim ExampleInput as Integer
ExampleInput = Val(InputBox("How Many Names do you want to put in?", "Input", 0)
Const ExampleConst as Integer = ExampleInput
 Dim ExampleArray(ExampleConst) as String
For x=0 to (ExampleArray.Length-1)
     ExampleArray(x)  =  InputBox("What is the name?",  "Names",  "John Smith")
Next



This works and allows you to have a array sized at runtime instead of having a set size.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

I don't know what you mean. All of these work fine and only one is a constant.
Dim x As Integer = 12
Dim y As Integer = 10
Dim XY(x, y) As String
Dim XY1(12, 10) As String
Const g As Integer = 12
Dim myArray(g)
Dim c As Integer = Val(InputBox("Enter a number"))
Dim cArray(c) As Integer

waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

@Shawn

If you use an arrayList like in the example link I have provided you can do the same thing with a lot more flexibility

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Dynamic Arrays can resize the capability of the Array at runtime

use redim

Initial declaration
Dim scores() As Integer

Resizing
ReDim scores(1)

check this.

http://vb.net-informations.com/collections/vb.net_dyanamic_array.htm

leveena

leveena
Newbie Poster
2 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You