Tip

American business forms

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.


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.

This was first published in August 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.