?
Solved

GetUpperBound and GetLowerbound

Posted on 2007-04-08
6
Medium Priority
?
540 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
[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
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

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 86

Expert Comment

by:Mike Tomlinson
ID: 18880342
Yes...where "highest" means get the greatest index number or slot number for the specified dimension.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

718 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