Looping Through Array

Ghoztriderr

New member
Joined
Jan 4, 2013
Messages
4
Programming Experience
1-3
Hi,

I've setup a simple array that I would like to loop through and compare the data in a text box on the form with one of the indexes in the array. If the field data matches one of the entries in the array I'll have it run some random code. Here is my code for reference:

VB.NET:
 'Create array for holding group members
            Dim Members(1) As String
            Members(0) = "Mary"
            Members(1) = "Larry"
           
            'Compare AccountName field to array
            For Each Mem In Members(2)
                If Mem = AccountName Then
                    nav.SelectSingleNode("/my:myFields/my:Current_User/my:CVT_Group", NamespaceManager).SetValue(TRUE)
                End If
            Next

When it reaches the For Each statement it is looping through each letter in the array index, for instance M, A, R, Y instead of looping through the complete word "Mary" and comparing it against the text field. Can someone point me in the right direction? Thanks in advance for any help!!
 
Try changing the For Each line to

For Each Mem As String In Members

Your current code is looking at the specific entry in the array and therefore the for each loop is going over the characters of that single entry.
 
Which part is not working? The for loop should now be looping over the items in the array. Note that the If statement inside your For loop will be case sensitive. Set a break point at the If statement and check the value of 'Mem'.
 
Hi,

When checking an array for the existence of an element, you do not need to use a loop, you can just use the Contains method of the array to test for the existence of an element. i.e:-

VB.NET:
Dim Members() As String = {"mary", "larry"}
If Members.Contains(TextBox1.Text.ToLower) Then
  MsgBox("Found!")
Else
  MsgBox("Not Found!")
End If

Notice that case sensitivity has been accommodated for here by entering the array elements in lower case and then ensuring any test made against the array in also converted to lower case first.

You also say that your code is still not working after Home Grown Coder's example. I think this is due to the fact that in your code you say that your AccountName is a field. If that field is a TextBox then you need to be comparing the Text property of the AccountName to the array and not the AccountName control.

Hope that helps.

Cheers,

Ian
 
Home Grown,

I ran through the code with the changes you recommended using the debugger. It ran with no errors, however, it was still looping through each character instead of each entry/word.

Ian,

AccountName is actually the variable I declared that ties to the Account_Name field/text box.I'm coding this in an InfoPath environment so I'm not quite sure that using the Text property will work here.
VB.NET:
Variable = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields", NamespaceManager).Value
is the only way I know how to pull the value from the text field in the form.

Not all is lost though. I tried a few more options.....dictionary, array, collection, and list. the only one I got to work thus far is the list option.

VB.NET:
'Create list for holding CVT group members
            Dim CVT As New List(Of String)
            CVT.Add("Larry")

I then looped through the list using for each and Bingo! It worked. Thank you both for your help. I'll take the code examples into consideration next time I need to code another form. Again Thanks.
 
I'd be curious to see the code that you tried, as I made a little sample and it was looping through each entry in the array.

IanRyder's comment about using Contains also applies to the List, so you don't need to loop over the contents of the list.
 
Basically what I started the thread with......

VB.NET:
'Create array for holding CVT group members            
      Dim Members(1) As String             
            Members(0) = "DOMAIN\Generic"             
            Members(1) = "DOMAIN\Generic1"                        

 'Compare AccountName field to array             
      For Each Mem As String In Members(1)                
           If Mem = AccountName Then                     
                nav.SelectSingleNode("/my:myFields/my:Current_User/my:CVT_Group", NamespaceManager).SetValue(TRUE)                 
           End If             
      Next
 
Remove the element from "Members(1)". A string is an array of characters. You are telling it to loop through each element of Members(1). Each element is a single character.

The statement should read:
VB.NET:
For Each Mem As String In Members
 
Back
Top