Solved

Arrays in VB.net 2005

Posted on 2009-05-06
10
527 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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