Solved

# GetUpperBound and GetLowerbound

Posted on 2007-04-08
527 Views
I need help understanding  GetUpperBound Method and GetLowerbound Method. Could you provide a simple sample on both functions?
0
Question by:VBdotnet2005
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 86

Expert Comment

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

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 86

Expert Comment

ID: 18880342
Yes...where "highest" means get the greatest index number or slot number for the specified dimension.
0

Author Comment

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 86

Accepted Solution

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

ID: 18895606
Idle_Mind:

Thank you again & again :)

0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
###### Suggested Courses
Course of the Month5 days, 11 hours left to enroll