Array Dimensions ?

From this example below I know that if I put in the subscripts 4,15 into this array that it returns a value of 1.

txtFields_Array(4, 15) = 1

The question is whether I can put in the value :

txtFields_Array (Subscript1,Subscript2) =  1
without the subscripts......


and return the subscript values 4 and 15 ?


Is this possible? I guess I am thinking in math terms and matrices.


Green_BoyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VbmasterCommented:
You can use a function like this...

  Function FindValue(x() As Long, Value as Long, a As Long, b As Long) As Boolean

    For a = LBound(x, 1) To UBound(x, 1)
      For b = LBound(x, 2) To UBound(x, 2)
        If (x(a, b) = Value) Then
          FindValue = True
          Exit Function
        End If
      Next
    Next

  End Function

You use it with code like...

  Dim Subscript1 As Long
  Dim Subscript2 As Long

  If FindValue(txtFields_Array, 1, Subscript1, Subscript2) Then
    Msgbox "Value found at position " & Subscript1 & "," & Subscript2
  End If
0
VbmasterCommented:
You will have to change some definitions depending on what kind of array you have, for example if it's a array with integers, just replace 'Value As Long' To 'Value As Integer', and 'x() As Long' to x() As Integer'.
0
BarryTiceCommented:
Green Boy --
Are you trying to determine which element in a two-dimensional array is equal to 1?

txtFields_Array(Subscript1,Subscript2) = 1
won't do it.

You'll need to loop through your array dimensions to determine this, and this gets more complicated if you have more than one element in your array equal to 1.

Dim intCount1 As Integer
Dim intCount2 As Integer
Dim intResults as Integer
Dim intSub1() as Integer
Dim intSub2() as Integer

Redim intSub1(1)
Redim intSub2(1)
IntResults = 0
For intCount1 = lbound(txtFields_Array,1) to Ubound(txtFields_Array,1)
    For intCount2 = Lbound(txtFields_Array,2) to Ubound(txtFields_Array,2)
        If txtFields_Array(intCount1, intCount2) = 1 then
            IntResults = intResults + 1
            IntSub1(intResults) = intCount1
            IntSub2(intResults) = intCount2
            Redim Preserve intSub1(intResults + 1)
            Redim Preserve intSub2(intResults + 1)
        End If
    Next intCount2
Next intCount1


Rem The above code will take the positions of each instance of 1 in your matrix
Rem and put that position in an array.

For intCount1 = 1 to intResults
    Me.Print intSub1(intCount1) & ", " & intSub2(intCount1)
Next intCount1

Rem The above code prints each ordered pair on your form.




Note: This code leaves the intSub1() and intSub2() arrays larger than they should be -- there are empty elements at intSub1(0), intSub2(0), intSub1(UBound(intSub1)), and intSub2(UBound(intSub2)), which you can clean up after the fact.

You should also check before the last part of the code that intResults isn't zero, which would indicate that "1" wasn't found anywhere in your matrix.

If you can be sure "1" won't be repeated anywhere in your array, or if you want to just fine one instance, this is overkill.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Green_BoyAuthor Commented:
Thanks Barry ......
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.