Solved

Arrays in VB.net 2005

Posted on 2009-05-06
10
526 Views
Last Modified: 2013-11-26
I have 2 questions in vb:
I am trying to declare an array of an indifinte size but i can't find this explained anywhere.

1. Can you declare an array of indefinite size but without initializing it?
 

Dim myArray() As Integer --> this does not work

Dim myArray() As Integer = {1,2,3} -> this works but this is not an indefinite? the array has ta size of 3

2. What is the "0" inside the following functions?

MsgBox(myArray.GetLowerBound(0))

MsgBox(myArray.GetUpperBound(0))

Thanks
0
Comment
Question by:jean11
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 39

Accepted Solution

by:
abel earned 65 total points
ID: 24319537
an array has by default a fixed size. If you want to change that, you have to redim it, which is a costly operation. Instead, it is better to use the datatypes List or ArrayList which provide a flexible size.
0
 

Author Comment

by:jean11
ID: 24319632
Do you mean there is no indefinite  array in vb.net?
0
 
LVL 39

Expert Comment

by:abel
ID: 24319670
I mean that there is no language where-ever that has an indefinite array (only on a turing machine, but that has indefinite memory), as long as you mean with indefinite infinitely growable or shrinkable.

Languages that do support growing / shrinkable array type (perl, ruby other scripting languages) do not really support the array type, but support the flexible array type.

The equivalent to that in VB.NET (and any .NET language) is to use the ArrayList. But since an arraylist is limited, you will quickly want to move on to the more versatile and very quick generic List interface, which does all you want and more.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:jean11
ID: 24319764
Thanks for the replies.

Yes I meant a growable array. An array that you do not specify size at the time of writing the program and  at run time the array will grow.

Do you know the answer to the second question?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 60 total points
ID: 24319799
As abel said, use a generic List...

Simplified:

    Dim myArray As New List(Of Integer)
    myArray.Add(4)
    myArray.Add(1)
    myArray.Add(1) ' <-- it "grows" automatically to accommodate new entries...
0
 
LVL 39

Expert Comment

by:abel
ID: 24319818
> 2. What is the "0" inside the following functions?
> MsgBox(myArray.GetLowerBound(0))

that is the dimension of the array. A classic array can have multiple dimensions. You can ask for each dimension what the upper bound is. It is something I very rarely encounter in normal encoding practices, but anyway, that's what it means ;)
0
 
LVL 23

Expert Comment

by:debuggerau
ID: 24322813
Sounds like the question was well answered except for the lowerbound(0) specific.

The (0) returns the lower bound for the indexes of the first dimension of the Array...
http://msdn.microsoft.com/en-us/library/system.array.getlowerbound(VS.71).aspx

0
 
LVL 23

Expert Comment

by:debuggerau
ID: 25193586
how'd you go?
0
 
LVL 39

Expert Comment

by:abel
ID: 25193911
> Sounds like the question was well answered except for the lowerbound(0) specific.
really? Think my last comment covered that. But good to put the pointer to the reference here.

Doin't expect this user to check back... If you don't ping the thread, you have a chance that it will be cleaned up by the clean up volunteers at some point (3 weeks minimum).
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question