?
Solved

VB.Net Loop to Create Variable Names

Posted on 2007-10-10
6
Medium Priority
?
1,113 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




0
Comment
Question by:msyed1
  • 3
  • 2
6 Comments
 
LVL 48

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 800 total points
ID: 20049041
Hi msyed1,

There's no easy way to do what your after with 12 seperate ArrayLists. What you can do to make it easier though, is store those arraylists in a HashTable, with the arraylist's name as the key...

        Private ArrayLists As New Hashtable ' declare at class level

        For i As Integer = 1 To 12
            ArrayLists.Add("arlFVIP" & i, New ArrayList)
        Next

Then, to add an item to a specific arraylist, you'd do something like this....

    CType(ArrayLists("arlFVIP9"), ArrayList).Add("whatever")

Then you can loop through the hashtable to run the same routine....

        For i As Integer = 1 To 12
            strFVIPUpdateStatus = DoFVIPUpdateOrInsert(CType(ArrayLists("arlFVIP" & i), ArrayList), intTrackingID, gaKeyerEmail))
        Next

Regards,

Wayne
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20049350
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
0
 

Author Comment

by:msyed1
ID: 20050151
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
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1200 total points
ID: 20050269
Sorry the code should have been like this

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

Author Comment

by:msyed1
ID: 20050322
Thankyou so much.  I am sure this will work.  Thanks again.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 20050499
Not a problem, glad to help. ;=)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question