If you have ever had to check if a value is part of a group of allowed values, but dread the thought of having to setup and array, and then do the looping to check if your new value is in the array, this shortcut is for you, just use a list so that you can use IsElement to test whether your new value is a list tag or not.
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
I had a case where I wanted to perform certain actions in a database QueryDelete event, but certain people should be excluded from the actions.
So, I could have created an array with the users who should be excluded, in which case the code snippet may have looked something like this.
Dim names() As String
Redim names(0) Names(0) = "Joe" . . Redim Preserve names(1) Names(1) = "Jill" . . Redim Preserve names(2) Names(2) = "Jocko" user = session.CommonName Forall n in names If n = user then ' a match was found Continue = True Exit Sub end if end forall
But since I don't enjoy Redim, and I really don't enjoy embedding of If's and For's, instead the above code became the following:
Dim allowed List As String allowed("Joe") = "Worker" allowed("Jill" ) = "Worker" allowed"Jocko") = "Boss" user = session.CommonName If Iselement(allowed( user )) = True Then ' match was found Continue = True Exit Sub End If
In the above example, the value I store in the list element really does not matter, I just care about the list tag so that I can use Iselement. I also remove the overhead associated with Redim's if I do not know how many names I will be adding. Finally, I reduce one level of nesting making the code much cleaner.