Solved

GetUpperBound and GetLowerbound

Posted on 2007-04-08
6
510 Views
Last Modified: 2008-02-01
I need help understanding  GetUpperBound Method and GetLowerbound Method. Could you provide a simple sample on both functions?
0
Comment
Question by:VBdotnet2005
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18874102
You pass the dimension of the array and the GetUpperBound() method returns the upper bound of that dimension.  The first dimension starts at 0 (zero).  The Array.Rank() method will tell you how many dimensions an array has.

GetLowerBound() always returns 0 (zero) assuming you have passed in a valid dimension for that array.

Here are some examples:

        Dim arr1(9) As Integer
        Debug.Print(arr1.Rank) ' 1
        Debug.Print(arr1.GetUpperBound(0)) ' 9

        Dim arr2(4, 5) As Integer
        Debug.Print(arr2.Rank) ' 2
        Debug.Print(arr2.GetUpperBound(0)) ' 4
        Debug.Print(arr2.GetUpperBound(1)) ' 5

        Dim arr3(7, 2, 6) As Integer
        Debug.Print(arr3.Rank) ' 3
        Debug.Print(arr3.GetUpperBound(0)) ' 7
        Debug.Print(arr3.GetUpperBound(1)) ' 2
        Debug.Print(arr3.GetUpperBound(2)) ' 6
0
 

Author Comment

by:VBdotnet2005
ID: 18880317
It looks like get the highest for GetUpperBound and get the lowest for GetLowerbound in array. Am I on the right track?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18880342
Yes...where "highest" means get the greatest index number or slot number for the specified dimension.
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:VBdotnet2005
ID: 18880398
Idle_Mind,

cool. I got that down. One thing I don't understand is Ok we know what the greatest index or lowerest index, knowing just what the index is how is that helping us?  Could you give me what we do exactly in the "real world"?
sample

       Dim arr1() As String = {"a", "b", "c"}
       
        For Each mystring As String In arr1

            MessageBox.Show(mystring) ' just for the hack of it


        Next

        MessageBox.Show(arr1.GetUpperBound(0)) '2  a is 0 b is 1 c is 2
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18891956
"how is that helping us?  Could you give me what we do exactly in the "real world"?"

Lol...a common usage is to setup a For...Next loop to iterate over the elements of an array:

        Dim arr1() As String = {"a", "b", "c"}
        Debug.Print("arr1")
        For i As Integer = 0 To arr1.GetUpperBound(0)
            Debug.Print("Index " & i & " = " & arr1(i))
        Next

        Dim arr2(,) As String = {{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k", "l"}}
        Debug.Print("arr2")
        For i As Integer = 0 To arr2.GetUpperBound(0)
            For j As Integer = 0 To arr2.GetUpperBound(1)
                Debug.Print("(" & i & "," & j & ") = " & arr2(i, j))
            Next
        Next
0
 

Author Comment

by:VBdotnet2005
ID: 18895606
Idle_Mind:

Thank you again & again :)

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net 2 44
LINQ - C# to VB convertion 12 58
SQL syntax in VB.net 5 39
Help with preventing selection from a combobox 11 28
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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