Group,

10 months ago I posed the same question as to how to programatically create the variable names via the loop. However I'm now in real need of doing this and I don't fully understand what each step of the syntax actually means and what it does. Therefore I need your help. Here is that original post: [https://www.daniweb.com/programming/software-development/threads/489038/loop-read-text-line-by-line-into-multiple-textboxes]

Here's what I currently have:

    Dim txtProps As String
    Dim i As Integer
    txtProps = ""
    Dim objReader As New System.IO.StreamReader(propFileName)
    i = 1
    If System.IO.File.Exists(propFileName) = True Then
        Do While objReader.Peek() <> -1
            txtProps = objReader.ReadLine()
            If txtProps = "" Then
                Exit Do
            End If
            If i = 1 Then
                prop01 = txtProps
            End If
            If i = 2 Then
                prop02 = txtProps
            End If
            If i = 3 Then
                prop03 = txtProps
            i = i + 1
            Loop

The reality is when the original post was written, I only needed 20 variable to put my property numbers in. But I'm rewriting this to allow all of our properties to be udated at one time. This means I need to have 120 variables. The common denominator is the prefix of the variable (prop). But I will need to name them "prop01", "prop02", etc. up through "prop120".

In Reverend Jim's reply he writes the following code:

      Dim data() As String = {"box1", "box2", "box3"}
      For i = 0 To 2
      Me.Controls("textBox" & (i + 1)).Text = data(i)
     Next

What I'm not understanding is, the "Dim data() As String = {"box1", "box2", "box3"}". Since I'm wanting to create variables that start with the word "prop", what information do I put in the "{}"? Do I put {"prop01", "prop02", "prop03"}? Since I need to define upwards of 120 (and more in the future), do I have to list all 120 variables? This is where I'm getting lost. I'm not sure what "Dim data() As String = {"box1", "box2", "box3"}" really means and does. Nor do I understand "Me.Controls("textBox" & (i + 1)).Text = data(i)".

Can you help me?

As always, thanks for your help.

Don

"Dim data() As String = {"box1", "box2", "box3"}"
Here box1,box2,box3 are not the names of the variables, they are string 3 values of the string array data().
You can declare it as
Dim data(2) As String
data(0)="box1"
data(1)="box2"
data(2)="box3"

But to store data in an array by using a loop you can use dynamic array. In every rotation you can redimention it to store New one by using Redim keyword before the array name.

I would use a list of string to create the variables. No need to use redim statements:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = "1" 'pevents error if textbox is empty
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim idList As New List(Of String)
        ListBox1.Items.Clear()
        'Create the variables
        Dim j As Integer = CInt(TextBox1.Text) - 1 'need to take 1 as list stats at 0
        Dim i As Integer
        For i = 0 To j
            idList.Add("Props" & i.ToString)
        Next
        'display variables in lisbox
        For i = 0 To idList.Count - 1
            ListBox1.Items.Add(idList.Item(i))
        Next

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub
End Class

Click Here

Minimalist, I believe I'm understand what you're doing here. It looks like

           Dim idList As New List(Of String)
            idList.Add("Props" & i.ToString)

is creating the variable on the fly. Now I need to get the property number from

         txtProps = objReader.ReadLine()

into this newly created variable. I tried

         idList.Add("Props" & i.ToString) = txtProps

and

        idList.Item(i) = txtProps

but it doesn't like either of these at all. So how do I insert the read text (txtProp) into this newly created variable? I'm not going to be able to put these into a textbox. In fact, I'm doing away with the textboxes altogether.

Again, thanks for the ideas. I'm thinking this is exactly what I need.

Don

I don't code VB, but from what I see the syntax, you should be doing idList.Add("Props" & txtProps.ToString). The first argument is the name and the one after & is the value. If you want to assign txtProps to the "Props" then you would need to assign it right there.

I think you are missing the point. If you just want to store a series (list) of strings (or numbers or whatever) you can declare a list and specify what you want it to be a list of. For example, if you want to store a list of strings you do

    Dim Props As New List(Of String)

    Props.Add("one")
    Props.Add("two")
    Props.Add("three")

    For i As Integer = 0 To Props.Count - 1
        Debug.WriteLine(Props(i))
    Next

The output in this case is

one
two
three

If you want a liist of integers you would do

    Dim Props As New List(Of Integer)

    Props.Add(412)
    Props.Add(634)
    Props.Add(18)

    For i As Integer = 0 To Props.Count - 1
        Debug.WriteLine(Props(i))
    Next

In this case the output is

412
634
18

You are not creating a list of variables. You are creating one variable which is a list of values.

Rev. Jim,

I may be misstating what I need to do. I know my phraseology may not be correct.

In my earlier version of my application I defined (Dim) a variable as a string. These variables are named "prop1, prop2, prop3" and so on through "prop50". Later in the code I use StreamReader to read a text file to retrieve the actual property numbers and store those numbers in the above variables.

I'm now having to grow this property list. I need for there to be a minimum of 120 (and rapidly growing) variables for the 120 different properties that I need to run some routines for.

Since I need so many variables, I'd like to create these variable names programatically. So instead of doing individual Dim statements for every variable, I'd prefer just a few lines of code that will create these for me based on the number of individual properties there are in the text file. Today there are 118. But in the next few weeks, there will be 124. A month later there will be 128.

So I'm trying to find the correct syntax (if this actually exists) to programatically create the variable "prop1" and then read the text file for the first line to get the first property and then store that property number in "prop1". Then via a loop and a counter, the next variable to be programatically created will be "prop2". Then the text file will be read to get line 2 and it's property number..... and so forth.

Hopefully I'm being a little clearer (someday I'll be better at my use of the correct terms).

Thanks again for the help.

Don

I'm still not getting through here. What you are suggesting is a way to create variables

prop001
prop002
prop003
.
.
.

What I am saying is that what you really want is

prop(1)
prop(2)
prop(3)
.
.
.

You want to declare a variable number of variables dynamically which, in most cases, is just bad programming. All of the variables you want to create are closely related. They are all of the same type and differ only in value. Yes, you can create certain types of variables dynamically - objects, but not the simple types like strings, integers, etc. In vbScript you can use ExecuteGlobal to do this. While there may be a way to do this in vb.net I am not aware of it and wouldn't recommend it even if I was.

One suggestion was to create a variable length array and use ReDim Preserve as needed. I would also not recommend this. It's clumsy and slow. Lists and Dictionaries are much more flexible and easier to use. If you want to post a small version of the file you are processing perhaps your intentions will be clearer and I can provide a code snippet to illustrate what I am saying.

commented: Not 120 variables for 120 values but 1 variable (array) for 120 values. This is the correct approach +11

On an unrelated note, if anyone is noticing frequent typos in my posts since last December (mostly consisting of repeated letters as in yoou and woulld) it is because Dell can seemingly not produce a laptop with a keyboard that is any better than complete shit.

But I'm not bitter

Here I mostly used your code but added a list to store your strings and a listbox to display these.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim idList As New List(Of String)
        Dim i As Integer = 1
        Dim txtProps As String = ""
        Dim objReader As New System.IO.StreamReader(propFileName)
        ListBox1.Items.Clear()
        If System.IO.File.Exists(propFileName) = True Then
            Do While objReader.Peek() <> -1
                txtProps = objReader.ReadLine()
                idList.Add("Props " & i.ToString & ", PropId  " & txtProps) 'add the properties to the list
                i += 1
            Loop
        End If

        'display strings in lisbox
        For i = 0 To idList.Count - 1
            ListBox1.Items.Add(idList.Item(i))
        Next
    End Sub

Now I am thinking... Are you trying to use the string as variable later on? Is it similar to intern or to_sym in Ruby???

Group,

Rev. Jim made a very good statement that I think I'm going to take: "You want to declare a variable number of variables dynamically which, in most cases, is just bad programming."

As a fairly new coder, I don't want to learn bad habits. So I'm going to abandon this idea and move on to more important things.

Group, as always I appreciate your help, input and above all, the good advice. You guys are the best!

Thanks again,

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.