Dim Variables in a Loop

Posted on 2006-05-16
Last Modified: 2012-05-05

I have the need to go through a loop to fill a listbox that I am dynamically creating.  I need to dim a new ListItem
each time as in (below):  I have just given a small sample here, the actual listbox is quite long and I want to do this in a loop if possible, but I don't know how to generate the variable names liAddItem1, liAddItem2, liAddItem3 etc. etc. going through a for-next loop.  Please show me how to do this in VB.Net.

                    Dim liAddItem1 As New ListItem
                    liAddItem1.Text = "Add Platform >>"
                    liAddItem1.Value = "001"

                    Dim liAddItem2 As New ListItem
                    liAddItem2.Text = "Edit Platform >>"
                    liAddItem2.Value = "002"

                    Dim liAddItem3 As New ListItem
                    liAddItem3.Text = "Delete Platform >>"
                    liAddItem3.Value = "003"
Thanks much. msyed1.
Question by:msyed1
    LVL 48

    Expert Comment

    You can use the same variable liAddItem for all items. Once item is added to the Items collection, you can reuse this variable again. Knowing this, you can write loop incrementing the Value and reading Text from some array.
    LVL 48

    Accepted Solution

    You can use also short form:

    lbxAddEditDel.Items.Add(New ListItem("Add Platform >>", "001"))

    Such line can be called in loop, providing different text and value by some way.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    You could do something like this...

    Dim TextValues As String = "Add Platform >>,Edit Platform >>,Delete Platform >>"
    Dim values() As String = TextValues.Split(",".ToCharArray)
    Dim li As ListItem
    Dim i As Integer
    For i = 1 to values.Length
        li = New ListItem
        li.Text = values(i-1)
        li.Value = i.ToString("000")
    Next i

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now