Learn how to a build a cloud-first strategyRegister Now


Dim Variables in a Loop

Posted on 2006-05-16
Medium Priority
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
  • 2
LVL 48

Expert Comment

ID: 16691957
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

AlexFM earned 700 total points
ID: 16691979
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 700 total points
ID: 16693240
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

810 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