We help IT Professionals succeed at work.

VB.Net Loop to Create Variable Names

msyed1
msyed1 asked
on
1,134 Views
Last Modified: 2012-06-22
Can someone PLEASE help me to do the below in a loop (for each or for next) so I don't have to repeat 12 times.  arlFVIP1 thru arlFVIP12  is an arraylist that can be populated or can be empty (nothing).  If it has values, then I need to go to a generic function for all 12 and bring back a status code... Thank you. msyed1.

 Dim strFVIPUpdateStatus As String

            If arlFVIP1.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP1, intTrackingID, gaKeyerEmail))
            End If

            If arlFVIP2.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP2, intTrackingID, gaKeyerEmail))
            End If

            If arlFVIP3.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP3, intTrackingID, gaKeyerEmail))
            End If

            If arlFVIP4.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP4, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP5.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP5, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP6.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP6, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP7.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP7, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP8.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP8, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP9.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP9, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP10.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP10, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP11.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP11, intTrackingID, gaKeyerEmail)
            End If

            If arlFVIP12.Count > 0 Then
                strFVIPUpdateStatus = DoFVIPUpdateOrInsert(arlFVIP12, intTrackingID, gaKeyerEmail)
            End If




Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi msyed1;

Here is another solution.

Create an array of ArrayList along with your 12 ArrayList as shown below

    Private arlFVIP_ALL(12) As ArrayList
    Private arlFVIP1 As New ArrayList
    Private arlFVIP2 As New ArrayList
    ...
    Private arlFVIP11 As New ArrayList
    Private arlFVIP12 As New ArrayList

In the form load event assign each ArrayList to a slot in the array of ArrayList as follows.

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load

        arlFVIP_ALL(1) = arlFVIP1
        arlFVIP_ALL(2) = arlFVIP2
        ...    
        arlFVIP_ALL(11) = arlFVIP11
        arlFVIP_ALL(12) = arlFVIP12

    End Sub

Now the code you posted can be written like this

    Dim strFVIPUpdateStatus As String

    For idx As Integer = 1 To arlFVIP_ALL.Length - 1
        If arlFVIP1.Count > 0 Then
            strFVIPUpdateStatus = _
                DoFVIPUpdateOrInsert(arlFVIP_ALL(idx), intTrackingID, gaKeyerEmail)
        End If
    Next

The nice thing about this is that you have two reference to the same array list. For example to get the length of arlFVIP1 you can do one of the following:

    Dim size As Integer = arlFVIP1.Length

or

    Dim size As Integer = arlFVIP_ALL(1).Length

Fernando

Author

Commented:
Webtubbs:  Thank you for your solution.  I haven't tried it yet....

FernandoSoto: is there a way I can avoid the use of spelling out arlFVIP1, 2, etc.  in the line where I have the arrow pointing ?  How can I generate that variable (arraylist) name ?
If I have to use the 1, 2, 3 etc.  I will still have to spell out all 12  ??

For idx As Integer = 1 To arlFVIP_ALL.Length - 1
        If arlFVIP1.Count > 0 Then       <=====================
            strFVIPUpdateStatus = _
                DoFVIPUpdateOrInsert(arlFVIP_ALL(idx), intTrackingID, gaKeyerEmail)
        End If
    Next
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thankyou so much.  I am sure this will work.  Thanks again.
Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Not a problem, glad to help. ;=)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.